How to add controls dynamically in windows form c#

November 16, 2014 , , 1 Comments

Visual studio provide the best features to design the form. By the designer window, you can add the controls from the toolBox. After added the items, you can set the properties by the property window. Same this things, you can do this by the code window. Follow some steps to add controls dynamically.

  Step-1 : Open Code window that is form1.cs file, create a object for controls like

TextBox t1 = new TextBox();

Step-2 : Associate properties of the TextBox class with the current object like

 t1.Name = "Text1";
    t1.Width = 300;
            t1.Text = " Dynamically added Control";
            t1.Location = new Point(10, 10);

Here Point is a class, in which you have to define the coordinates of x-axis and y-axis, where you want to add the TextBox.

Step-3 : Add this instance in the form by following line of code.


 Now code Generate the following output:
How to add controls dynamically in windows form c#

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

1 comment:

  1. Blogs are so informative where we get lots of information on any topic. Nice job keep it up!!
    A1 English Test Pakistan