You can add and remove controls in a control array at run time using the Load and Unload statements. However, the control to be added must be an element of an existing control array. You must have created a control at design time with the Index property set, in most cases, to 0. Then, at run time, use this syntax:
Load object(index%)
Unload object(index%)
Argument | Description |
object | Name of the control to add to or delete from the control array. |
index% | The control's index value in the array. |
When you load a new element of a control array, most of the property settings are copied from the lowest existing element in the array — in this example, the element with the 0 index value. The Visible, Index, and TabIndex property settings are not automatically copied to new elements of a control array, so to make the newly added control visible, you must set its Visible property to True.
Note Visual Basic generates an error if you attempt to use the Load statement with an index number already in use in the array.
Important You can use the Unload statement to remove any control created with Load. However, you cannot use Unload to remove controls created at design time, regardless of whether or not they are part of a control array.