Control Array:
Suppose you have a form with 100 command buttons and they all accomplish pretty
much the same task -- they print their caption to a label control. Now what would be
better; to draw 100 command buttons and name them btnPrint1 through btnPrint100, or
should you find a better way to do it? In this scenario you would undoubtedly use a
control array.
A control array is exactly what the name implies -- an array of controls. So instead of
100 different names, you would have btnPrint(1) through btnPrint(100).
much the same task -- they print their caption to a label control. Now what would be
better; to draw 100 command buttons and name them btnPrint1 through btnPrint100, or
should you find a better way to do it? In this scenario you would undoubtedly use a
control array.
A control array is exactly what the name implies -- an array of controls. So instead of
100 different names, you would have btnPrint(1) through btnPrint(100).
Setting up a Control Array
(i) In a control array, there is one control which the other controls take after when
they are created. Each control in a control array also has an index so we can
determine which one is to be used. You assign the index of each control by
giving the control a value in it's .index property. If a control has a value in it's
.index property, then it is assumed by the program to belong to a control array.
(ii) Every control in the control array also has the same name, and they are
distinguished as different contro ls through their index property. Let's set up a
program with 5 command buttons that are part of a control array. When you
click on one of the 5 buttons a notice will be displayed in a label indicating
which button has been clicked.
they are created. Each control in a control array also has an index so we can
determine which one is to be used. You assign the index of each control by
giving the control a value in it's .index property. If a control has a value in it's
.index property, then it is assumed by the program to belong to a control array.
(ii) Every control in the control array also has the same name, and they are
distinguished as different contro ls through their index property. Let's set up a
program with 5 command buttons that are part of a control array. When you
click on one of the 5 buttons a notice will be displayed in a label indicating
which button has been clicked.
Object Property & its Values
button .name=btnControl .index=0 .caption="Button &1"
button .name=btnControl .index=1 .caption="Button &2"
button .name=btnControl .index=2 .caption="Button &3"
button .name=btnControl .index=3 .caption="Button &4"
button .name=btnControl .index=4 .caption="Button &5"
label .name=lblNotice .caption=""
Notice that the indexes start from 0 and go to 4, but the captions indicate the button's
number is one greater than it's index. This is taken into account when we write code
for the controls.
button .name=btnControl .index=0 .caption="Button &1"
button .name=btnControl .index=1 .caption="Button &2"
button .name=btnControl .index=2 .caption="Button &3"
button .name=btnControl .index=3 .caption="Button &4"
button .name=btnControl .index=4 .caption="Button &5"
label .name=lblNotice .caption=""
Notice that the indexes start from 0 and go to 4, but the captions indicate the button's
number is one greater than it's index. This is taken into account when we write code
for the controls.
All the controls in a control array share the same code, and each event of a
control in a control array is passed the control's index. So this index argument
can be used in the code, as is shown below:
Sub btnControl_Click (Index As Integer)
lblNotice = "You selected button " & Str$(Index + 1)
End Sub
Note: We're just setting the caption of lblNotice to indicate which button we
pushed. We add one to Index because the first index is 0, and the caption of the first
button indicates that it is number 1. So we take this into account by adding one to the
index.
control in a control array is passed the control's index. So this index argument
can be used in the code, as is shown below:
Sub btnControl_Click (Index As Integer)
lblNotice = "You selected button " & Str$(Index + 1)
End Sub
Note: We're just setting the caption of lblNotice to indicate which button we
pushed. We add one to Index because the first index is 0, and the caption of the first
button indicates that it is number 1. So we take this into account by adding one to the
index.