Object and Control Arrays

One of the most interesting things you can do with objects is to declare an array of objects. For example, you can declare an array of command buttons or forms. Moreover, these objects don't even have to exist. For example, you don't have to declare all forms at design time, because you can still create an array of forms at runtime.

You already know about the Forms and Printers collections. Visual Basic also supports the Controls collection, which lets you step through all your controls as though they were array variables. For example, the following code hides all controls:

For intCtr = 0 to Controls.Count - 1

Controls(intCtr).Visible = False

Next intCtr

If your application contains multiple forms, you can hide all controls on all forms by using a nested loop (notice that For Each eliminates the Count - 1 requirement):

  1. Dim frmAForm As Form
  2. Dim ctlAControl As Control
  3. For Each frmAForm In Forms ' Step through all forms
  4. For Each ctlAControl In frmAForm.Controls
  5. ctlAControl.Visible = False
  6. Next ctlAControl
  7. Next frmAForm

caution

A menu is considered a control in the Controls collection. In many situations, you'll want to omit the menu controls in such a loop by testing with the TypeOf() function to determine whether the control is a Menu object before setting its visibility to False.

The Controls collection holds all controls on your current form; however, you can declare a control array to hold one specific type of control. You declare an array of controls as follows:

Dim ctlManyLabels(1 To 4) As Label

The next section discusses collections further. Collections work a lot like arrays in that you can access individual elements in the collections just as you can with arrays. You might want to create an array of objects, such as forms and controls. Rather than create the objects at design time, you can create the objects in an array as follows (notice the New keyword):

Dim frmArray(1 To 10) As New frmFirstForm

This Dim statement assumes that one form, frmFirstForm, exists. After the declaration, 10 new forms exist, subscripted from frmArray(1) to frmArray(10). Subsequent code can then change the form properties of the forms in the array to make each form different from the base form, named frmFirstForm.

note

None of these forms will appear until you invoke their Show methods.

Suppose you want to decrease the font size of a form's controls if a user resizes a maximized form. You can use the Controls collection to decrease the font size of all controls:

  1. Private Sub Form_Resize ()
  2. ' Decrease all the controls' font size
  3. Dim intCtr As Integer
  4. For intCtr = 0 to Controls.Count - 1
  5. Controls(intCtr).FontSize = Controls(intCtr).FontSize * .75
  6. Next intCtr
  7. End Sub

Each control's font size will now be 25 percent smaller than it was before the user resized the form.

You won't see many Visual Basic programmers using control arrays when a collection exists for the same object (Visual Basic supplies a Forms predefined collection). If you want to use control arrays, however, you have to declare memory to hold the array contents and to initialize the arrays.

Visual Basic supports one technique for control arrays that you'll find yourself using a lot, even though collections are always available to you. When you copy a control and paste that control back onto the form, Visual Basic displays the message box shown in Figure 16.5.

Figure 16.5

Visual Basic will create a control array for you.

You might wonder why you'd ever copy and paste a control, but if you need to place several command buttons or labels that all have the same format - perhaps the same font size and caption alignment - it's a helpful technique. You just create one control, set all its properties, copy that control to the Clipboard, and then paste the Clipboard contents onto the form to add as many controls as you need.

As soon as you paste the copied control, Visual Basic displays the message box shown in Figure 16.5. If you answer Yes, Visual Basic automatically creates a control array with a name that matches the first control. For example, if the first control is a command button named Command1, the array is named Check1, and the elements begin at Command1(0) and increment as long as you keep pasting the control.

Your code then can step through all the control array elements from Command1(0) through Command1(n), where n is the total number of Check1 controls on the form, and set properties for them.

Top Home