Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
Dynamically adding controls to a Windows Form in .NET - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on Web, HTML5, Mobile, MonoTouch for iPhone, MonoDroid for Android, and Windows Azure.

News

Personal Blog

Work Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

Dynamically adding controls to a Windows Form in .NET

I just wrote some code to dynamically add some textboxes to a Windows Form in .NET.  I thought I would share it here and see if anyone has any suggestions, since this is my first attempt at dynamically adding elements to a Windows Form in .NET. 

Basically within the form's class, I create an ArrayList, as a private variable in the form's class.  On the Load event of the form, I instantiate the ArrayList and then add TextBox objects to the ArrayList.  Remember that you need to actually add the control to the form by using this.Controls.Add().  On an event, such as a button click, you can get the values of the text boxes as shown in the button click event below.  If you have any suggestions, please put them in as feedback.

//Start Code

private ArrayList arylTxtGEItems;
//VS.NET stuff omitted for brevity.
private void frmEncounterDynamic_Load(object sender, System.EventArgs e)
{
int i = 0;
this.arylTxtGEItems = new ArrayList();
for(i = 0; i< 3; i++)
{
      this.arylTxtGEItems.Add( new System.Windows.Forms.TextBox() );
      ((System.Windows.Forms.TextBox)this.arylTxtGEItems[i]).Location = new System.Drawing.Point(40, 36 + i * 20);
      ((System.Windows.Forms.TextBox)this.arylTxtGEItems[i]).Name = "txtGE" + i.ToString();
      ((System.Windows.Forms.TextBox)this.arylTxtGEItems[i]).Size = new System.Drawing.Size(184,20);
      ((System.Windows.Forms.TextBox)this.arylTxtGEItems[i]).TabIndex = i + 2;
      ((System.Windows.Forms.TextBox)this.arylTxtGEItems[i]).Text = String.Empty;
      this.Controls.Add( ((System.Windows.Forms.TextBox)this.arylTxtGEItems[i]) );
}
}

private void button1_Click(object sender, System.EventArgs e)
{
      MessageBox.Show( ((System.Windows.Forms.TextBox)this.arylTxtGEItems[1]).Text );
}

//End Code

Wally

Posted: Mar 30 2004, 10:40 AM by Wallym | with 5 comment(s)
Filed under:

Comments

CC said:


I'm wondering if the arraylist is really necessary? And also all of those casts in frmEncounterDynamic_Load look kinda ugly, couldn't you just instantiate a textbox, do all the stuff and THEN add it to the arraylist (if you need to keep it).
# March 30, 2004 10:52 AM

Fabrice said:

You don't like temp variables, do you?
# March 30, 2004 12:24 PM

Bibek said:

Hello i want to retrieve the value of the textbox created dynamically.for eg.

for (i=0;i<5;i++)

{

 string value=value of textbox(txt+i)

}

how is it possible.

# February 13, 2007 12:34 AM

Andrew said:

Great, thanks for the help.

I've used a custom button as by dynamically added control

I just have 1 question:

How can you now use the click event of the added controls?

# August 3, 2007 12:36 PM

Andrew said:

I did this in c#

I created a custom button, add it to an ArrayList and also add an EventHandler :

(in Wally's example above, put this in that for loop)

   myControl.Click += new System.EventHandler(DynamicControl_Click);

I then created a new method called "DynamicControl_Click"

///////////////////////////////

private void DynamicControl_Click(object sender, EventArgs e)

       {

           myConrols.myButton myButton = (myConrols.myButton)sender;

           //you now know which button was clicked

           String s = myButton.Name;

       }

//////////////////////

the DynamicControl_Click method will now catch all the click events of all the dynamically added buttons or which ever control you've added so you may want to put a switch statement in there to work out which btn was actualy clicked.

# August 3, 2007 2:27 PM