Control.ControlCollection.AddRange(Control[]) Method

Definition

Adds an array of control objects to the collection.

C#
public virtual void AddRange(System.Windows.Forms.Control[] controls);
C#
public virtual void AddRange(params System.Windows.Forms.Control[] controls);

Parameters

controls
Control[]

An array of Control objects to add to the collection.

Examples

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.

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

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(Control[]) in a derived class, be sure to call the base class's AddRange(Control[]) method to ensure that the controls are added to the collection.

Applies to

Продукт Версії
.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