Click here to Skip to main content
12,501,890 members (53,558 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: .NET3.0 VB .NET
friends tell me the method to dynamically create controls
Posted 8-Dec-09 6:39am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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);
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160919.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100