3. Exploring the Visual Basic Toolbox
Control Arrays
- With some controls, it is very useful to define control arrays
- it depends on the application. For example, option buttons are almost
always grouped in control arrays.
- Control arrays are a convenient way to handle groups of controls that
perform a similar function. All of the events available to the single
control are still available to the array of controls, the only difference
being an argument indicating the index of the selected array element is
passed to the event. Hence, instead of writing individual procedures for
each control (i.e. not using control arrays), you only have to write one
procedure for each array.
- Another advantage to control arrays is that you can add or delete
array elements at run-time. You cannot do that with controls (objects)
not in arrays. Refer to the Load and Unload statements in
on-line help for the proper way to add and delete control array elements
at run-time.
- Two ways to create a control array:
- Create an individual control and set desired properties.
Copy the control using the editor, then paste it on the form.
Visual Basic will pop-up a dialog box that will ask you if you
wish to create a control array. Respond yes and the array is created.
- Create all the controls you wish to have in the array.
Assign the desired control array name to the first control.
Then, try to name the second control with the same name.
Visual Basic will prompt you, asking if you want to create
a control array. Answer yes. Once the array is created,
rename all remaining controls with that name.
- Once a control array has been created and named, elements of the
array are referred to by their name and index. For example, to set the
Caption property of element 6 of a label box array named
lblExample, we would use:
lblExample(6).Caption = "This is an example"
We'll use control arrays in the next example.
Counter Hit
This Homepage is special brought to you by CK Tan