FAtechs is a Web Site where you can find a lot of things related to tech and Education. Our Posts are mostly about Programming, Information, Technology,Software, Reviews & Everything You need to Know. You can also find Books and Novels here. Your appreciation and criticism will be welcomed.
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:
=newPoint(x, y);// location can be changed by changing
the value of X and Y.
=newSystem.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular,
txtbx.Tag ="dynamic"; // giving a tag so we can differentiate it later
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 = newLinkLabel;
increment = 50;
for (int d = 0; d < 10; d++)
label[d] = newLinkLabel();
label[d].AutoSize = true;
label[d].Location = newPoint(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;
First name from Full name using our own made method.
label[d].Click += newEventHandler((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#:
publicvoid label_click(object sender, EventArgs e, DataGridView dgv)
label_clic = true;
LinkLabel label = (sender asLinkLabel);
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.