Index Property (Control Arrays)

See Also1XEPWS2              Example76MJ9ST>Low

Applies To

Check box9P3BU5, combo box1YZXFF6, command buttonXJSPC0, common dialog1H1HYJI, data control2E1FEX3, directory list boxO9U5A0, drive list box5WJO0PW, file list box1M6S8UX, frame1KX6ZP8, grid2VGT0PT, horizontal scroll bar1JSJOS7, image9A4FCA, label3MNIZ8D, lineTW530P, list boxG11UCK, menuBKDT1F, OLE control2HQDVVU, option buttonJYBO08, picture box31MYIWX, shape9JZFLA, text boxYPYZDG, timer3MVR08J, vertical scroll bar1JSJOS7.

Description

Specifies the number that uniquely identifies a control in a control array8G7Y0UU.  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 Name2016V9P 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 LoadGU0759 statement or remove them with the Unload17FKE8Z 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

IntegerDOKXHY


See Also

Help:

Creating a Control ArrayBRU4A5

Creating a Menu Bar1DSHCUG

Index Object1WDWMJT

Index PropertyH12OL9 (Data Access)

Tag Property6GI6KP

 

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