Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make control array in code

Tags:

vb6

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 precise. 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 backcolor to see all my label boxes. This code does not work. I get an error "Object variable or With block variable not set". Any help? I don't know what is wrong. I would like to keep the label boxes in a control array I know how to do it without making it a control array.

like image 917
ashraj98 Avatar asked Apr 16 '13 22:04

ashraj98


People also ask

How can you create a control array?

You set up a control array by naming one or more controls of the same type the same name and set the Index property of each control in the array to a non-negative value (i.e., the controls in the control array are usually indexed from 0 to one less than the number of controls in the array).

What is control array how it is created?

In Visual Basic, a control array is a group of related controls in a Visual Basic form that share the same event handlers. Control arrays are always single-dimensional arrays, and controls can be added or deleted from control arrays at runtime.

Which array contains all the controls in a form?

The FormArray APIcontrols: This is an array containing all the controls that are part of the array. length: This is the total length of the array. at(index): Returns the form control at a given array position.


1 Answers

Cody Gray had it correct in his comment. I don't believe you can create a control array on the fly only in code in VB6. You have to place one instance of the control on the form and give it an Index property value of zero. This creates a control array with only one element, at index zero. You can then modify your code to produce the desired result, like so:

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's 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

As noted in the comment, you don't need to load another instance of the control at array index zero because you did that at design time. I also iterated my array starting from zero for slightly easier calculation of the indices.

like image 109
Bob Mc Avatar answered Oct 01 '22 02:10

Bob Mc