Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Control.ControlCollection.Add Method

Adds the specified control to the control collection.

[Visual Basic]
Public Overridable Sub Add( _
   ByVal value As Control _
)
[C#]
public virtual void Add(
 Control value
);
[C++]
public: virtual void Add(
 Control* value
);
[JScript]
public function Add(
   value : Control
);

Parameters

value
The Control to add to the control collection.

Exceptions

Exception Type Condition
Exception The specified control is a top-level control, or a circular control reference would result if this control were added to the control collection.
ArgumentException The object assigned to the value parameter is not a Control.

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 object 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 in a derived class, be sure to call the base class's Add method to ensure that the control is added to the collection.

Example

[Visual Basic, C#, C++] The following example adds a Control to the Control.ControlCollection of the derived class Panel. The example assumes 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.

[Visual Basic] 
' Create a TextBox to add to the Panel.
Dim TextBox1 As TextBox = New TextBox()

' Add controls to the Panel using the Add method.
Private Sub AddButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles AddButton.Click
    Panel1.Controls.Add(TextBox1)
End Sub

[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);
}

[C++] 
// Create a TextBox to add to the Panel.
private:
TextBox* textBox1;

// Add controls to the Panel using the Add method.
void addButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
    textBox1 = new TextBox();
    panel1->Controls->Add(textBox1);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Control.ControlCollection Class | Control.ControlCollection Members | System.Windows.Forms Namespace | Remove | RemoveAt | AddRange

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.