Index Property (Control Arrays)
See Also Example
Applies To
Check box, combo box
Description
Specifies the number that uniquely identifies a control in a control array Available only if the control is part of a control array; read-only at run time.
Usage
[form.]control [( integer )].Index
Setting
The Index property settings are:
Setting Description
No value (Default) Not part of an array.
0 to 32,767 Part of an array. Specify an integer greater than or equal to 0 to assign a control to a control array. You can specify the same name for two or more controls through the Name property. Visual Basic automatically assigns the first unique index available within the control array.
Remarks
Because control array elements share the same Name property setting, you must use the Index property in code to refer to a particular control in the array. The Index must appear as a number in parentheses next to the control array namefor example:
MyButtons(3)
When a control in the array recognizes that an event has occurred, Visual Basic calls the array's event procedure and passes the applicable Index as an additional argument. This property is also used when you create controls dynamically at run time with the Load statement or remove them with the Unload statement.
With menu control arrays, the first menu item must have an index of 0, and the following menu items must have indexes numbered contiguously from top to bottom.
Note To remove a control from a control array, change the control's Name property setting.
Data Type
Integer
Help:
Creating a Control Array
Creating a Menu Bar
Index Object
Index Property (Data Access)
Tag Property
Programmer's Guide:
Chapter 3, "Creating and Using Controls"
Chapter 4, "Menus and Dialogs"
Index Property (Control Arrays) Example
The example starts with two option buttons and adds new option buttons to the form each time you click a command button. When you click an option button, the FillStyle property is set and a new circle is drawn. To try this example, paste the code into the Declarations section of a form that has two option buttons (set the Name property to optButton for both buttons to create an array), a command button, and a large picture box.
Sub OptButton_Click (Index As Integer)
Dim H, W ' Declare variables.
Picture1.Cls ' Clear picture.
Picture1.FillStyle = Index ' Set FillStyle.
W = Picture1.ScaleWidth / 2 ' Get size of circle.
H = Picture1.ScaleHeight / 2
Picture1.Circle (W, H), W / 2 ' Draw circle.
End Sub
Sub Command1_Click ()
Static MaxIdx ' Largest index in array.
If MaxIdx = 0 Then MaxIdx = 1 ' Preset MaxIdx.
MaxIdx = MaxIdx + 1 ' Increment index.
If MaxIdx > 7 Then Exit Sub ' Put 8 buttons on form.
Load OptButton(MaxIdx) ' Create new item in array.
' Set location of new option button under previous button.
OptButton(MaxIdx).Top = OptButton(MaxIdx - 1).Top + 360
OptButton(MaxIdx).Visible = True ' Make new button visible.
End Sub