Adding Controls Dynamically in C#

Adding Controls Dynamically in C# (Adding Controls at Run Time):

Adding the Controls dynamically means that you want to add some Controls like Text Boxes, Labels, Combo Boxes at Run time. Suppose you want to add Text boxes for many products but you don't have any idea about the number of products at the time of coding and you want to add Text boxes in an application when customer select that how many product he/she is going to buy. This can easily be done by adding the Controls in your Form or Panel Dynamically using C#.

The examples given below will help you in Adding the Controls at Run time. Now we will see the example of Adding a Text box dynamically:

Code to Add TextBox Dynamically in C#:
       int x = 240, y = 300;

       TextBox txtbx = new TextBox(); 

       txtbx.Size = new System.Drawing.Size(173, 100); //setting size
       txtbx.Location = new Point(x, y); // location can be changed by changing the value of X and Y.

       txtbx.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

       txtbx.Tag = "dynamic"; // giving a tag so we can differentiate it later
       txtbx.ForeColor = Control.DefaultForeColor;

       panel1.Controls.Add(txtbx); //panel1 is the panel on form                      

You can add multiple controls at a time using for loop and Arrays of Control dynamically. Now I am demonstrating here an example of a LinkLabel by adding the control through loop dynamically in C#. You have to set different location of each loop by changing it the x-axis or y-axis to avoid overlapping of the controls. All link labels can have a single click method and each method will run according to the LinkLabel being clicked. We also put an extra parameter other the the default ones. The LinkLabel generation code and the method of click event is given below. By using this code you can easily add any control dynamically in C# Windows form.

Code to Add Multiple Labels Dynamically in C#:

LinkLabel[] label = new LinkLabel[10];
              int increment = 50;

for (int d = 0; d < 10; d++)
            label[d] = new LinkLabel();
            label[d].AutoSize = true;
            label[d].Location = new Point(10, 15 + c);
            label[d].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label[d].Tag = "dynamic"; // giving a tag so we can differentiate it later
            label[d].LinkColor = Color.Black;
            label[d].Text = name_separator(list[i].ToString()); //separating First name from Full name using our own made method.
            label[d].Click += new EventHandler((s, e) => label_click(s, e, dgv)); // click event of that specific label with an extra parameter dgv(DataGridView name).. you can pass your own according to requirement.

            c += 700 i//nt in Y-axis by 700 points in every new label


Code of method used by all the Labels in C#:

public void label_click(object sender, EventArgs e, DataGridView dgv)
            label_clic = true
            LinkLabel label = (sender as LinkLabel);


We hope the above tutorial will provide help in adding the Windows Form Controls dynamically using code on run time. You may ask more in comments or through our Facebook Page. You may contact us for writing or suggesting posts for us.

Learn Simple C# Code to send email here
Learn how to use DataGridView in C# here

Hit Like Button if you understood, It helps a lot to keep us Motivated.


Post a Comment