Control.ControlCollection.Add(Control) Method

Definition

Adds the specified control to the control collection.

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

Parameters

value
Control

The Control to add to the control collection.

Exceptions

The specified control is a top-level control, or a circular control reference would result if this control were added to the control collection.

The object assigned to the value parameter is not a Control.

Examples

The following code example adds a Control to the Control.ControlCollection of the derived class Panel. The example requires that you have created a Panel control and a Button control on a Form. When the button is clicked, a TextBox control is added to the panel's Control.ControlCollection.

C#
// Create a TextBox to add to the Panel.
private TextBox textBox1 = new TextBox();

// Add controls to the Panel using the Add method.
private void addButton_Click(object sender, System.EventArgs e)
{
   panel1.Controls.Add(textBox1);
}

Remarks

The Add method allows you to add Control objects to the end of the control collection.

You can also add new Control objects to the collection by using the AddRange method.

To remove a Control that you previously added, use the Remove, RemoveAt, or Clear methods.

Note

A Control can only be assigned to one Control.ControlCollection at a time. If the Control is already a child of another control it is removed from that control before it is added to another control.

Notes to Inheritors

When overriding Add(Control) in a derived class, be sure to call the base class's Add(Control) method to ensure that the control is added to the collection.

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