A control array is a group of controls that share the same name type and the same
event procedures. Adding controls with control arrays uses fewer resources than
adding multiple control of same type at design time.
Creating Control Arrays at Design Time
There are three ways to create a control array at design time.
-
Assigning the same name in the Name property for more than one control.
-
Copying an existing control and then pasting it to the Form.
-
Setting the Index property to a value that is not null.
Adding a Control Array at Run Time
Control arrays can be created at run time using the statements
-
Load object (Index %)
-
Unload object (Index %)
Where object is the name of the control to add or delete from the control array.
Index % is the value of the index in the array. The control array to be added
must be an element of the existing array created at design time with an index
value of 0. When a new element of a control array is loaded, most of the property
settings are copied from the lowest existing element in the array.
Following example illustrates the use of the control array.
* Open a Standard EXE project and save the Form as Calculator.frm and
save the Project as Calculater.vbp.
* Design the form as shown below.
Object
|
Property |
Setting
|
| Form |
Caption
Name |
Calculator
frmCalculator |
| CommandButton |
Caption
Name
Index |
1
cmd
0 |
| CommandButton |
Caption
Name
Index |
2
cmd
1 |
| CommandButton |
Caption
Name
Index |
3
cmd
2 |
| CommandButton |
Caption
Name
Index |
4
cmd
3 |
| CommandButton |
Caption
Name
Index |
5
cmd
4 |
| CommandButton |
Caption
Name
Index |
6
cmd
5 |
| CommandButton |
Caption
Name
Index |
7
cmd
6 |
| CommandButton |
Caption
Name
Index |
8
cmd
7 |
| CommandButton |
Caption
Name
Index |
9
cmd
8 |
| CommandButton |
Caption
Name
Index |
0
cmd
10 |
| CommandButton |
Caption
Name
Index |
.
cmd
11 |
| CommandButton |
Caption
Name |
AC
cmdAC |
| CommandButton |
Caption
Name |
+
cmdPlus |
| CommandButton |
Caption
Name |
-
cmdMinus |
| CommandButton |
Caption
Name |
*
cmdMultiply |
| CommandButton |
Caption
Name |
/
cmdDivide |
| CommandButton |
Caption
Name |
+/-
cmdNeg |
| TextBox |
Name
Text |
txtDisplay
( empty ) |
| CommandButton |
Caption
Name |
=
cmdEqual |

The following variables are declared inside the general declaration
Dim Current As Double
Dim Previous As Double
Dim Choice As String
Dim Result As Double
The following code is entered in the cmd_Click( ) (Control Array)
event procedure
Private Sub cmd_Click(Index As Integer)
txtDisplay.Text = txtDisplay.Text & cmd(Index).Caption
'&is the concatenation operator
Current = Val(txtDisplay.Text)
End Sub
The following code is entered in the cmdAC_Click ( ) event procedure
Private Sub cmdAC_Click()
Current = Previous = 0
txtDisplay.Text = ""
End Sub
The below code is entered in the cmdNeg_Click( ) procedure
Private Sub cmdNeg_Click()
Current = -Current
txtDisplay.Text = Current
End Sub
The following code is entered in the click events of the cmdPlus,
cmdMinus, cmdMultiply, cmdDevide controls respectively.
Private Sub cmdDevide_Click()
txtDisplay.Text = ""
Previous = Current
Current = 0
Choice = "/"
End Sub
Private Sub cmdMinus_Click()
txtDisplay.Text = ""
Previous = Current
Current = 0
Choice = "-"
End Sub
Private Sub cmdMultiply_Click()
txtDisplay.Text = ""
Previous = Current
Current = 0
Choice = "*"
End Sub
Private Sub cmdPlus_Click()
txtDisplay.Text = ""
Previous = Current
Current = 0
Choice = "+"
End Sub
To print the result on the text box, the following code is entered
in the cmdEqual_Click ( ) event procedure.
Private Sub cmdEqual_Click()
Select Case Choice
Case "+"
Result = Previous + Current
txtDisplay.Text = Result
Case "-"
Result = Previous - Current
txtDisplay.Text = Result
Case "*"
Result = Previous * Current
txtDisplay.Text = Result
Case "/"
Result = Previous / Current
txtDisplay.Text = Result
End Select
Current = Result
End Sub
Save and run the project. On clicking digits of user's choice
and an operator button, the output appears.
( Download the source code
)