Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.0 VB
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 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  
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 | :cool:
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 255
1 Sergey Alexandrovich Kryukov 150
2 BotCar 105
3 Richard MacCutchan 100
4 ChauhanAjay 95
0 Sergey Alexandrovich Kryukov 9,056
1 OriginalGriff 8,041
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2014
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