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

5 Comments



  • 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).

  • You don't like temp variables, do you?

  • 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.

  • 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?

  • 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.

Comments have been disabled for this content.