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

Control.ControlCollection.Add Method

Adds the specified control to the control collection.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public virtual void Add (
	Control value
)
public void Add (
	Control value
)
public function Add (
	value : Control
)

Parameters

value

The Control to add to the control collection.

Exception typeCondition

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.

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.

NoteNote

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

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.

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

// 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.get_Controls().Add(textBox1);
} //addButton_Click

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.