How do I fill three listboxes without writing the same
code three times ?
It's used at Form_Load(), VB 4.0 (16).
[i] Thanks for some help, Matt-D(eutschland) :) ;)
Printable View
How do I fill three listboxes without writing the same
code three times ?
It's used at Form_Load(), VB 4.0 (16).
[i] Thanks for some help, Matt-D(eutschland) :) ;)
There are several approaches you could use
1.
2.Code:'Assumes you have a control array of listboxes eg list1(0)
'list1(1) and list1(2)
Dim i as integer
For i = 0 to 2
list1(i).addItem "Item in listbox " & Trim$(Str$(i))
Next
I haven't actually tested the second example and i have a feeling that you might need a set statement in there somewhere but give it a go anywayCode:'Passes the name of each list box to a sub that fills it
fillListBox list1
fillListBox list2
fillListBox list3
Private Sub fillListBox(name as listbox)
name.addItem "Item 1"
name.addItem "Item 2"
End Sub
You could do it this way as well (example adding 0-10 to all listboxes):
Code:Private Sub Form_Load()
Dim MyControl As Control
For Each MyControl In Me.Controls
For i = 0 To 10
If TypeOf MyControl Is ListBox Then MyControl.AddItem i
Next i
Next
End Sub
Or you can create a Control Array of ListBoxes and use the following to add to them.
Code:For lst = List1.lBound To List1.uBound
For I = 0 To 10
List1(lst).AddItem "Number" & I
Next I
Next lst