Index Property (Control Arrays)
See Also Example
Check box, combo box, command button , common dialog , data control , directory list box , drive list box , file list box , frame , grid , horizontal scroll bar , image , label , line , list box , menu , OLE control , option button , picture box , shape , text box , timer , vertical scroll bar .
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..
[form.]control [( integer )].Index
The Index property settings are:
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.
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:
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 LoadGU0759 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.
Creating a Control Array
Creating a Menu Bar
Index Property (Data Access)
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.
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.