Arrays, Part II

by Michael Anderson

Summary of Section 7.3

7.3: Arrays of Control Objects;
Creating arrays: Index property
Copying array elements: Ctrl+C and Ctrl+V
Event procedures: "Index As Integer"
Dynamic array creation: Load statement; Load event
Dynamic positioning: .Height & .Width
Making elements visible: .Visible property

Motivation for Arrays of Control objects

Arrays of control objects (ie. user interface elements) for one main reason: they allow us to match all the coding structures of the programs with corresponding user interface elements.

We will use examples of a keypad and a simple typewriter problem (Q. 36, p. 346) to illustrate exactly how convenient these arrays can be.

 

Creating control arrays: Index property

Unlike data arrays, control arrays are not created using a "Dim" statement.

Control arrays must be started by choosing one control object, and setting its Index property to 0. This is the signal to VB that we will be creating an array.

There is no particular upper limit on the number of elements in a control array; we can continue adding new elements as long as we like.

Copying array elements: Ctrl+C and Ctrl+V

Additional elements can be created manually by copying and pasting using the standard Ctrl+C (copy) and Ctrl+V (paste).

Be sure the desired control is selected (click on it) then copy it!

All pasted copies will be utterly and totally identical except for the position and index value. Hint: make sure the control object is "correct" before you start copying it!

You will probably want to modify one or more properties of each newly created copy, eg. the Caption property

Event procedures: "Index As Integer"

Ordinary (non-array) objects have familiar looking event procedures: eg. Private Sub cmdButton_Click()

Event procedures for array objects can be distinguished by an extra parameter in the header line:
Private Sub cmdArrayButton_Click( Index As Integer )

The Index parameter can be used to determine exactly which button caused the event to be triggered. eg. Calculator keys.

Dynamic array creation: Load statement; Load event

MAJOR SHIFT IN THINKING!!! In addition to the manual method for creating array objects (see above), it is also possible to create then under control of a program. Most properties can also be changed under control of the program.

Array elements are created programatically with the "Load" statement, eg. Load cmdArrayButton( 2 )
or Load cmdArrayButton( i )

Any array element created programatically:
(1) should have it's position adjusted
(2) must be set to "visible" if you want to see it !!!

Do not confuse the "Load" statement with the "Load" event (for forms). Note, however, that Load statements are commonly found within the code for the Load event.

Dynamic positioning: .Height & .Width

Every object has the properties of .Top .Left .Height .Width

An object's position is determined by the coordinates of it's top left corner.

The units of measure for positioning are twips (1440 twips per inch; these units are only(?) used by Microsoft).

Make use of the "j = i + 1" paradigm for positioning objects.

Making elements visible: .Visible property

WARNING!!! As noted above, any object created programatically is initially invisible! If you want to see it, then you should set it's .Visible property to True.