Results 1 to 4 of 4

Thread: How to fill three listboxes with one code

  1. #1

    Thread Starter
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305

    Arrow


    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)



  2. #2
    Addicted Member
    Join Date
    Aug 1999
    Location
    Hamilton, New Zealand
    Posts
    137
    There are several approaches you could use

    1.
    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
    2.
    Code:
    '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
    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 anyway

    Regards

    Matt Brown
    Hamilton, NZ
    VB6 C++ in Visual Studio 6sp4
    VB.net Beta 1

  3. #3
    Guest
    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

  4. #4
    Guest
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width