AddRange Method

Control.ControlCollection.AddRange Method (Control())

 

Adds an array of control objects to the collection.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Overridable Sub AddRange (
	controls As Control()
)

Parameters

controls
Type: System.Windows.Forms.Control()

An array of Control objects to add to the collection.

The Control objects contained in the controls array are appended to the end of the collection.

You can use the AddRange method to quickly add a group of Control objects to the collection instead of manually adding each Control to the collection using the Add method.

To remove a Control that you previously added, use the Remove, RemoveAt, or Clear methods.

Notes to Inheritors:

When overriding AddRange in a derived class, be sure to call the base class's AddRange method to ensure that the controls are added to the collection.

The following code example adds two Control objects to the Control.ControlCollection of the derived class Panel. The example requires that you have created a Panel control and a Button control on a Form. When the button is clicked, two RadioButton controls are added to the panel's 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

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft