Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Auto generate labels and textboxes dynamically when single or multiple items are selected in CheckedListbox in a windows form.Labels must contain the text of selected items.
Posted
Updated 9-Dec-13 0:32am
v3
Comments
joginder-banger 9-Dec-13 6:33am    
Share more information what type of error you getting.
Aydin Homay 9-Dec-13 6:45am    
Could you explain what is your meaning of auto generate ? are you want create dynamic(run time) label or text box or ...?
uditCsharp 9-Dec-13 7:14am    
Exactly...I want dynamically created labels.Actually i have a checkedlistbox in a form that contains several items.What i want is when i check\select one or multiple items in checkedlistbox,label or labels(depends on the number of items selected) display the text of selected item.
Aydin Homay 9-Dec-13 7:49am    
Use solution#1 you just need to the add comfortable checkedlistbox event same select event and call controls creation method after deciding with switch case or if commands about selected item ;-)
OriginalGriff 9-Dec-13 6:53am    
And?
Your question is?
What do you need help with - because at the moment this is just a list of instructions...

1 solution

Hi
You can create any control at run time and add from parent control same as form(this) for example:

C#
Label label = new Label()
{
    Name = "lblTest",
    Size = new Size(100, 20),
    ForeColor = Color.Black,
    Location = new Point(50, 50),
    Text = "Test"
};
this.Controls.Add(label);


for getting more information please follow of links:

Adding Controls to Windows Forms
Adding Controls to Windows Forms at Runtime

Best Regards.
 
Share this answer
 
v4
Comments
BillWoodruff 9-Dec-13 8:12am    
Do be aware that a Label created with these parameters at run-time will result in nothing visible being shown on the Form or ContainerControl in which it is hosted.
Aydin Homay 9-Dec-13 8:15am    
You need to the set Text property I updated solution check it again please ;-) of course down vote is not good idea when you are wrong !
Member 1880403 9-Dec-13 8:56am    
Sorry, but BillWoodruff is correct. By default, controls added at run time in this way are invisible. The simple one line addition needed to make your example work is to set Visible = true;
BillWoodruff 10-Dec-13 10:27am    
fyi: I did not down-vote your response.
Aydin Homay 10-Dec-13 12:57pm    
;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900