This documentation is archived and is not being maintained.

Control.ControlCollection.AddRange Method

Adds an array of control objects to the collection.

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

Parameters

controls
An array of Control objects to add to the collection.

Remarks

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.

Example

[Visual Basic, C#, C++] The following example adds two Control objects 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, two RadioButton controls are added to the panel's Control.ControlCollection.

[Visual Basic] 
' Create two RadioButtons to add to the Panel.
Dim RadioAddButton As RadioButton = New RadioButton()
Dim RadioAddRangeButton As RadioButton = New RadioButton()

' Add controls to the Panel using the AddRange method.
Private Sub AddRangeButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles AddRangeButton.Click
    ' Set the Text the RadioButtons will display.
    RadioAddButton.Text = "RadioAddButton"
    RadioAddRangeButton.Text = "RadioAddRangeButton"

    ' Set the appropriate location of RadioAddRangeButton.
    RadioAddRangeButton.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, RadioAddRangeButton})
End Sub

[C#] 
// 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});
}

[C++] 
// Create two RadioButtons to add to the Panel.
private:
RadioButton* radioAddButton;
RadioButton* radioRemoveButton;

// Add controls to the Panel using the AddRange method.
void addRangeButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
    radioAddButton = new RadioButton();
    radioRemoveButton = new RadioButton();

    // Set the Text the RadioButtons will display.
    radioAddButton->Text = S"radioAddButton";
    radioRemoveButton->Text = S"radioRemoveButton";

    // Set the appropriate location of radioRemoveButton.
    radioRemoveButton->Location = System::Drawing::Point(radioAddButton->Location.X, 
        radioAddButton->Location.Y + radioAddButton->Height);

    //Add the controls to the Panel.
    Control* controlArray[] = {radioAddButton, radioRemoveButton};
    panel1->Controls->AddRange(controlArray);
}

[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

See Also

Control.ControlCollection Class | Control.ControlCollection Members | System.Windows.Forms Namespace | Add | CopyTo

Show: