Make a control array in code

This code does not work. I want to create a control array on my Form_Load in VB6, because I have to make 225 of them for a scrabble board, and they have to be accurate. My code is:

Private lblblocks(1 To 225) As Label

Private Sub Form_Load()
Dim i As Integer, j As Integer
For i = 1 To 15
    For j = 1 To 15
        Dim arrnum As Integer
        arrnum = (i - 1) * 15 + j
        Load lblblocks(arrnum)
        With lblblocks(arrnum)
            .Width = 1000
            .Height = 1000
            .Top = (i - 1) * 1000
            .Left = (j - 1) * 1000
            .Visible = True
            .BackColor = Int(Rnd(1) * 255) + &H100 * Int(Rnd(1) * 255) + &H10000 * Int(Rnd(1) * 255)
        End With
    Next j
Next i
End Sub

I used the back to see all my shortcuts. This code does not work. I get the error "Object variable or With block variable not set". Any help? I do not know what is wrong. I would like to store labels in a control array, I know how to do this without making it a control array.

+5
source share
1 answer

. , VB6. Index, . . , :

Private Sub Form_Load()
    Dim i As Integer
    Dim j As Integer

    For i = 0 To 14
        For j = 0 To 14
            Dim tileIdx As Integer
            tileIdx = i * 15 + j

            'If the tile index is zero, we already have that control,
            'so there no need to load new instance. Otherwise, use the 
            'Load method to create a new control in the array with the 
            'specified index.
            If tileIdx > 0 Then
                Load lblTile(tileIdx)
            End If

            With lblTile(tileIdx)
                .Width = 1000
                .Height = 1000
                .Top = i * 1000
                .Left = j * 1000
                .Visible = True
                .BackColor = Int(Rnd(1) * 255) + &H100 * Int(Rnd(1) * 255) + &H10000 * Int(Rnd(1) * 255)
            End With
        Next
    Next
End Sub

, , . , , .

+6

All Articles