Form.ControlCollection.Add(Control) Method

Definition

Adds a control to the form.

C#
public override void Add(System.Windows.Forms.Control value);
C#
public override void Add(System.Windows.Forms.Control? value);

Parameters

value
Control

The Control to add to the form.

Exceptions

A multiple document interface (MDI) parent form cannot have controls added to it.

Examples

The following code example adds a TextBox and Label control to the control collection of a form. The example requires that a form has been created and named Form1.

C#
public void AddMyControls()
 {
    TextBox textBox1 = new TextBox();
    Label label1 = new Label();
    
    // Initialize the controls and their bounds.
    label1.Text = "First Name";
    label1.Location = new Point(48,48);
    label1.Size = new Size (104, 16);
    textBox1.Text = "";
    textBox1.Location = new Point(48, 64);
    textBox1.Size = new Size(104,16);
 
    // Add the TextBox control to the form's control collection.
    Controls.Add(textBox1);
    // Add the Label control to the form's control collection.
    Controls.Add(label1);
 }

Remarks

You can use this method to add controls to the form. If you want to add a group of already created controls to the form, use the Control.ControlCollection.AddRange method of the Control.ControlCollection class.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also