Export (0) Print
Expand All

Control.ControlCollection.AddRange Method

Adds an array of control objects to the collection.

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

public virtual void AddRange (
	Control[] controls
)
public void AddRange (
	Control[] controls
)
public function AddRange (
	controls : Control[]
)

Parameters

controls

An array of Control objects to add to the collection.

The Control objects contained in the controls array are appended to the end of the collection.

You can use the AddRange method to quickly add a group of Control objects to the collection instead of manually adding each Control to the collection using the Add method.

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

Notes to Inheritors When overriding AddRange in a derived class, be sure to call the base class's AddRange method to ensure that the controls are added to the collection.

The following code example adds two Control objects 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, two RadioButton controls are added to the panel's Control.ControlCollection.

// Create two RadioButtons to add to the Panel.
private RadioButton radioAddButton = new RadioButton();
private RadioButton radioRemoveButton = new RadioButton();

// Add controls to the Panel using the AddRange method.
private void addRangeButton_Click(object sender, System.EventArgs e)
{
   // Set the Text the RadioButtons will display.
   radioAddButton.Text = "radioAddButton";
   radioRemoveButton.Text = "radioRemoveButton";
			
   // Set the appropriate location of radioRemoveButton.
   radioRemoveButton.Location = new System.Drawing.Point(
     radioAddButton.Location.X, 
     radioAddButton.Location.Y + radioAddButton.Height);
			
   //Add the controls to the Panel.
   panel1.Controls.AddRange(new Control[]{radioAddButton, radioRemoveButton});
}

// Create two RadioButtons to add to the Panel.
private RadioButton radioAddButton = new RadioButton();
private RadioButton radioRemoveButton = new RadioButton();

// Add controls to the Panel using the AddRange method.
private void addRangeButton_Click(Object sender, System.EventArgs e)
{
    // Set the Text the RadioButtons will display.
    radioAddButton.set_Text("radioAddButton");
    radioRemoveButton.set_Text("radioRemoveButton");

    // Set the appropriate location of radioRemoveButton.
    radioRemoveButton.set_Location(new System.Drawing.Point(radioAddButton.
        get_Location().get_X(), radioAddButton.get_Location().get_Y() 
        + radioAddButton.get_Height()));

    //Add the controls to the Panel.
    panel1.get_Controls().AddRange(new Control[] { radioAddButton, 
        radioRemoveButton });
} //addRangeButton_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

Community Additions

ADD
Show:
© 2014 Microsoft