Control.ControlCollection.AddRange 方法

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Overridable Sub AddRange ( _
    controls As Control() _
)
用法
Dim instance As ControlCollection
Dim controls As Control()

instance.AddRange(controls)
public virtual void AddRange (
    Control[] controls
)
public:
virtual void AddRange (
    array<Control^>^ controls
)
public void AddRange (
    Control[] controls
)
public function AddRange (
    controls : Control[]
)

参数

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

备注

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

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

若要移除以前添加的 Control,请使用 RemoveRemoveAtClear 方法。

给继承者的说明 在派生类中重写 AddRange 时,要确保调用基类的 AddRange 方法,以保证将控件添加到集合中。

示例

下面的代码示例将两个 Control 对象添加到派生类 PanelControl.ControlCollection 中。该示例要求已在 Form 上创建了一个 Panel 控件和一个 Button 控件。当单击该按钮时,将把两个 RadioButton 控件添加到面板的 Control.ControlCollection 中。

' 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
// 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;
   RadioButton^ radioRemoveButton;

   // Add controls to the Panel using the AddRange method.
   void addRangeButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      radioAddButton = gcnew RadioButton;
      radioRemoveButton = gcnew RadioButton;
      
      // Set the Text the RadioButtons will display.
      radioAddButton->Text = "radioAddButton";
      radioRemoveButton->Text = "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.
      array<Control^>^controlArray = {radioAddButton,radioRemoveButton};
      panel1->Controls->AddRange( controlArray );
   }
// 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Control.ControlCollection 类
Control.ControlCollection 成员
System.Windows.Forms 命名空间
Add