Arrays of Controls
Control arrays are
very simple to use, they are mostly used like variable arrays. If an
control is a member of a array, instead of his usual members, it
exports the Count, Item,UBound,LBound members. This members are
describer in the following table:
| Count |
Returns the number of controls in the array from witch the
member is. |
| Item([index]) |
Returns the control’s object specified by the index. This is
also the default property. Example (instead of
[controlname].Item(1).Caption = “OK” we can use
[controlname](0).Caption = “Ok”) |
| UBound |
Exacly like the UBound function for arrays of variables |
| LBound |
Exacly like LBound function for arrays of variables |
Creating a control array from design
mode
To create a control
array at design time you just have to set the name two controls, of
the same type, identical (for example two labels can have the name
“lblInfo”). The order of the elements in the array is the order
you have drawn the elements on the form. Anyway this order is not
fixed, you can change it using the index property of the objects.
Obs: All
controls have the index, top, left, (name) and tooltiptext properties.
Creating a control array from design
mode
To create a control
array at design time you just have to set the name two controls, of
the same type, identical (for example two labels can have the name
“lblInfo”). The order of the elements in the array is the order
you have drawn the elements on the form. Anyway this order is not
fixed, you can change it using the index property of the objects.
Obs: All
controls have the index, top, left, (name) and tooltiptext properties.
Creating and/or
resizing a control array at run time
First you must create
the first element of your array at design time and set it’s index
property to 0. Now let’s create the next element of our array from
code.
We will use the Load
function. Let’s say you wanted to create an array of labels: We
will draw, a label on our form and name it “lblArray” and a button
naming it “cmdAddArrayItem”.
The code:
Private
Sub cmdAddArrayItem_Click()
Dim NewItem As Integer
NewItem
= lblArray.UBound + 1
Load
lblArray(NewItem)
With
lblArray(NewItem - 1)
lblArray(NewItem).Top = .Top + .Height
lblArray(NewItem).Caption = "test"
lblArray(NewItem).Visible = True
End
With
End Sub |
This
example will create more labels on your form and displaying them one
under the other. |