Control.ControlCollection.AddRange(Control[]) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Lägger till en matris med kontrollobjekt i samlingen.
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())
Parametrar
Exempel
I följande kodexempel läggs två Control objekt till i Control.ControlCollection den härledda klassen Panel. Exemplet kräver att du har skapat en Panel kontroll och en Button kontroll på en Form. När knappen klickas läggs två RadioButton kontroller till i panelens 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
Kommentarer
Objekten Control som finns i matrisen controls läggs till i slutet av samlingen.
Du kan använda AddRange metoden för att snabbt lägga till en grupp med objekt i samlingen i stället för att manuellt lägga till var Control och en i samlingen med hjälp av ControlAdd metoden.
Om du vill ta bort en Control som du tidigare har lagt till använder du Removemetoderna , RemoveAteller Clear .
Anteckningar till arvingar
När du AddRange(Control[]) åsidosätter i en härledd klass måste du anropa basklassens AddRange(Control[]) metod för att säkerställa att kontrollerna läggs till i samlingen.