Control.ControlCollection.AddRange(Control[]) Metoda

Definicja

Dodaje tablicę obiektów sterujących do kolekcji.

public:
 virtual void AddRange(cli::array <System::Windows::Forms::Control ^> ^ controls);
public virtual void AddRange (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())

Parametry

controls
Control[]

Tablica Control obiektów do dodania do kolekcji.

Przykłady

Poniższy przykład kodu dodaje dwa Control obiekty do Control.ControlCollection klasy Panelpochodnej . Przykład wymaga utworzenia kontrolki Panel i kontrolki Button w obiekcie Form. Po kliknięciu przycisku zostaną dodane dwie RadioButton kontrolki do panelu 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

Uwagi

Obiekty Control zawarte w tablicy controls są dołączane na końcu kolekcji.

Za pomocą AddRange metody można szybko dodać grupę Control obiektów do kolekcji zamiast ręcznie dodawać je Control do kolekcji przy użyciu Add metody .

Aby usunąć Control element, który został wcześniej dodany, użyj Removemetod , RemoveAtlub Clear .

Uwagi dotyczące dziedziczenia

Podczas zastępowania AddRange(Control[]) w klasie pochodnej należy wywołać metodę klasy AddRange(Control[]) bazowej, aby upewnić się, że kontrolki są dodawane do kolekcji.

Dotyczy

Zobacz też