What is the method for creating an array of VB controls?

To create a VB control array, you need to first declare a control array variable and then use the ReDim statement in the code to define the size of the array. Here is an example:

Dim myArray() As Control

Private Sub Form_Load()
    Dim i As Integer
    
    ReDim myArray(0 To 4) ' 定义数组大小为0到4
    
    For i = 0 To 4
        Set myArray(i) = Me.Controls.Add("VB.TextBox", "TextBox" & i) ' 创建文本框控件并添加到数组中
        With myArray(i)
            .Left = 100
            .Top = 100 + i * 30
            .Width = 100
            .Visible = True
        End With
    Next i
End Sub

In this example, we declared a control array variable named myArray and used the ReDim statement in the Form_Load event to define the array’s size as 0 to 4. We then used a loop to create five text box controls and added them to the myArray array. By setting properties such as Left, Top, and Width for each text box, we were able to position and style these text box controls.

bannerAds