Visual Basic Concepts

Working with Control Arrays

A control array is a group of controls that share the same name and type. They also share the same event procedures. A control array has at least one element and can grow to as many elements as your system resources and memory permit; its size also depends on how much memory and Windows resources each control requires. The maximum index you can use in a control array is 32767. Elements of the same control array have their own property settings. Common uses for control arrays include menu controls and option button groupings.

Note   Visual Basic includes the ability to dynamically add unreferenced controls to the Controls collection at run time. This topic refers only to referenced controls added at design time by cutting and pasting a control onto the form. For more information about adding controls at run time, see the reference topic "Add Method (Controls Collection)" and "Add Method (Licenses Collection)."

Why Use Control Arrays?

Adding controls with control arrays uses fewer resources than simply adding multiple controls of the same type to a form at design time. Control arrays are also useful if you want several controls to share code. For example, if three option buttons are created as a control array, the same code is executed regardless of which button was clicked.

If you want to create a new instance of a control at run time, that control must be a member of a control array. With a control array, each new element inherits the common event procedures of the array.

Using the control array mechanism, each new control inherits the common event procedures already written for the array. For example, if your form has several text boxes that each receive a date value, a control array can be set up so that all of the text boxes share the same validation code.

Sample Application: Calc.vbp

The Calculator sample application (which is listed in the Samples directory) shown in Figure 7.2 contains two control arrays — the number buttons and the operator buttons.

Figure 7.2   Control array example

The Name and Index property values for the control arrays in the Calculator example are listed in the following table.

Number(n) Operator(n)
0 = Number(0) + = Operator(1)
1 = Number(1) – = Operator(2)
2 = Number(2) X = Operator(3)
3 = Number(3) / = Operator(4)
4 = Number(4) = = Operator(5)
5 = Number(5)  
6 = Number(6)
7 = Number(7)
8 = Number(8)
9 = Number(9)

Notice how each control is referred to with the syntax object(index). You specify the index of a control when you create it. In fact, specifying any index for a control at design time makes that control part of an array.

The Index property distinguishes one element of the control array from another. When one of the controls in the array recognizes an event, Visual Basic calls a common event procedure and passes an argument (the value of the Index property) to identify which control actually recognizes the event.

For example, the first line of the Number_Click event procedure is:

Private Sub Number_Click (Index As Integer)

If Number(0) recognizes the event, Visual Basic passes 0 as the index argument, and if Number(1) recognizes the event, Visual Basic passes 1 as the index argument. Other than the index value, the remainder of the Number_Click code that is executed is the same for both Number(0) through Number(9).