Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.33/5 (4 votes)
See more:
friends tell me the method to dynamically create controls
Posted

I'm making a couple of assumptions here:
1. You're using Visual Studio.
2. This is a Winforms project.

One simple, albeit slightly hacky, way is to create the controls you want at design time and copy and paste the code generated in the "designer" classes into a method you will call at run time. Then just delete the controls from visual designer.
 
Share this answer
 
Create the control and add it to parent container..

TextBox tb = new TextBox();

panel.Controls.Add(tb);



You should also need to set properties to the control to position it correctly.

Cheers
:cool:
 
Share this answer
 
DropDownList ddlday = new DropDownList();
DropDownList ddlmonth = new DropDownList();
DropDownList dllyear = new DropDownList();
Button btn = new Button();
Label lbl = new Label();
lbl.ID = "lbl";
lbl.Text = "date";
dllyear.ID = "ddlyear";
ddlmonth.ID = "ddlmonth";
ddlday.ID = "ddlday";
btn.ID = "btnsave";
btn.Text = "Save";
for (int i = 1; i < 32; i++)
{
ddlday.Items.Add(i.ToString());
}
for (int i = 1; i < 13; i++)
{
ddlmonth.Items.Add(i.ToString());
}
for (int i = System.DateTime.Now.Year - 20; i < System.DateTime.Now.Year + 1; i++)
{
dllyear.Items.Add(i.ToString());
}

Panel1.Controls.Add(ddlday);
Panel1.Controls.Add(ddlmonth);
Panel1.Controls.Add(dllyear);
btn.Click += new EventHandler(btnsave_Click);
Panel1.Controls.Add(btn);
 
Share this answer
 

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