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

定義

將控制項物件陣列加入至集合。

public:
 virtual void AddRange(cli::array <System::Windows::Forms::Control ^> ^ controls);
public:
 virtual void AddRange(... cli::array <System::Windows::Forms::Control ^> ^ controls);
public virtual void AddRange (System.Windows.Forms.Control[] controls);
public virtual void AddRange (params System.Windows.Forms.Control[] controls);
abstract member AddRange : System.Windows.Forms.Control[] -> unit
override this.AddRange : System.Windows.Forms.Control[] -> unit
Public Overridable Sub AddRange (controls As Control())
Public Overridable Sub AddRange (ParamArray controls As Control())

參數

controls
Control[]

要加入至集合中之 Control 物件的陣列。

範例

下列程式碼範例會將兩 Control 個 物件新增至 Control.ControlCollection 衍生類別 Panel 的 。 此範例會要求您已在 上 Form 建立 Panel 控制項和 Button 控制項。 按一下按鈕時,會將兩 RadioButton 個控制項新增至面板的 Control.ControlCollection

   // 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.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.
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

備註

Control陣列中包含的 controls 物件會附加至集合結尾。

您可以使用 AddRange 方法快速將物件群組 Control 新增至集合,而不是使用 Add 方法手動將每個 Control 物件新增至集合。

若要移除 Control 您先前新增的 Remove ,請使用 、 RemoveAtClear 方法。

給繼承者的注意事項

AddRange(Control[]) 衍生類別中覆寫時,請務必呼叫基類的 AddRange(Control[]) 方法,以確保控制項已新增至集合。

適用於

另請參閱