Control Arrays

Creating a Control Array

A control array is a collection of identical interface objects. Each object in the group shares the same object name, so the entire group can be selected and defined at once.

However, the objects in a control array can also be referenced individually, giving you complete control over each of the items in your user interface.

Creating an Array

We can create a control array by just copying the interface object and then pasting new objects onto our form.

What this does is to create an identical object but at the same time assign it a new number so that it can be separately referenced by our program code.

Loops and Arrays

By combining our array with a loop we can make a very powerful program procedure.

Let’s give it a try by modifying and simplifying our Lotto program.

Instead of the several lines of code we needed in the past, we can now accomplish the same thing with just a few lines of code.

 

New Lotto Code

Dim A

A = Array(0, 1, 2, 3, 4, 5, 6, 7, 8)

For i = 0 To 8

A(i) = Int(Rnd * 39 + 1)

Label1(i).Caption = A(i)

Next i

 

Index Numbers and Arrays

At times you will want to modify the array index numbers (those numbers that specify the individual array elements). Normally they start with 0 (for example in our lotto program Label box 1 is named Label1(0)).

This can be done by changing the Index property.

 

Exit Statements in For…Next

An Exit statement allows you to exit a For…Next loop before the loop has finished execution.

This allow you to respond to a specific event that occurs before the loop runs the preset number of times.

For example, we can cause the program to stop if the user enters in specific data.

 

Exit Statements

The following will exit when the user inputs the word "Done" even if the loop does not complete.

For I=1 to 10

InpName=InputBox("Enter your name or type Done to Quit.")

If InpName = "Done" Then Exit For

Print InpName

Next i

 

Do Loops

Do loops are alternatives to For…Next loops.

Do loops execute a group of statements until a certain condition is True in the loop.

For example we could use a Do loop in out last example to let users enter names until they enter the word "Done."

 

Do Loops cont...

For example:

Do While InpName<>"Done"

InpName=InputBox("Enter your name or type Done to quit.")

If InpName<>"Done" Then Print InpName

Loop

If we wanted the loop to run at least once then we put the conditional test at the bottom. (Loop While InpName<>"Done")

 

Do Loops cont...

In Do loops we can use the Until expression instead of the While expression to execute a loop until the user inputs something specific or the program reaches a specific point.

Ex. Loop Until InpName="Done"