Vandad Mohaddesi
Vandad Mohaddesi

Reputation: 25

how to define arrays of lists in vb6?

I want to declare an array of Listbox in vb6 and I need help to do it. I have tried this code:

Dim list() As ListBox

but has the following error:

object variable or with block variable not set 91

Upvotes: 0

Views: 2327

Answers (1)

Boann
Boann

Reputation: 50021

If you give two or more controls the same "(Name)" property in the form designer, VB will prompt you to create a control array, which might be what you want.

If you need to create an array manually, remember you have to dimension it. E.g.,

Dim list(0 To 9) As ListBox

Or:

Dim list() as ListBox
...
ReDim list(0 To 9) As ListBox

But you also have to put ListBoxes in the array. When you first dimension the array, you only get the array itself. All its entries are set to Nothing, which is what is meant by "Object variable or With block variable not set". You have to Set each array entry to a valid ListBox before you can use it.

If you want to add controls to a form at run time, use Controls.Add. For example:

For i = LBound(list) To UBound(list)
    Set list(i) = Controls.Add("VB.ListBox", "List" & i, Me)
    list(i).Visible = True

    list(i).AddItem "hello"
    list(i).Move 0, list(i).Height * i
Next

Upvotes: 3

Related Questions