Control.ControlCollection.AddRange(Control[]) 方法

定义

将控件对象数组添加到集合中。

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

参数

controls
Control[]

要添加到集合中的 Control 对象的数组。

示例

下面的代码示例将两个 Control 对象添加到 Control.ControlCollection 派生类 Panel。 该示例要求你已创建控件PanelButton控件。Form 单击按钮时,会将两个 RadioButton 控件添加到面板的 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});
}

注解

Control数组中包含的controls对象将追加到集合的末尾。

可以使用该方法AddRange快速将一组Control对象添加到集合,而不是使用Add该方法手动将每个Control对象添加到集合中。

若要删除之前添加的、Control使用RemoveRemoveAtClear方法。

继承者说明

AddRange(Control[]) 派生类中重写时,请务必调用基类 AddRange(Control[]) 的方法,以确保将控件添加到集合中。

适用于

产品 版本
.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
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅