Results 1 to 4 of 4

Thread: Listbox .ListCount/.List Issue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Posts
    566

    Listbox .ListCount/.List Issue

    Code:
    Private Sub Form_Load()
         With List1
             .AddItem "test1"
             .AddItem "test2"
             .AddItem "test3"
             .AddItem "test4"
             .AddItem "test5"
         End With
    End Sub
    
    Private Sub Command1_Click()
         Dim i As Integer
         For i = 0 To List1.ListCount 'tried i = 1 to ...
              MsgBox List1.List(i)
              Exit Sub
         Next i
    End Sub
    It only returns the first string in the Listbox and not ALL the strings. I cannot figure this out.

    Any suggestions?
    if you helped me, consider yourself thanked

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Listbox .ListCount/.List Issue

    For i = 0 To List1.ListCount - 1

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Listbox .ListCount/.List Issue

    ..and remove the "Exit Sub"

  4. #4
    Lively Member
    Join Date
    Oct 2007
    Posts
    68

    Re: Listbox .ListCount/.List Issue

    Looks like you're exiting your sub after it meets the first condition. So you'll never get to I = 1 or beyond. Plus it needs to be to listcount -1.
    Code:
         For i = 0 To List1.ListCount 'tried i = 1 to ...
              MsgBox List1.List(i)
              Exit Sub
         Next i
    If you want to list all elements in a messagebox, you could copy all the elements to a string field and then display the msgbox when done.

    Code:
    Private Sub Command1_Click()
         Dim i As Integer
         dim stest as string 
    
         For i = 0 To List1.ListCount -1 
              stest = stest & list1(i) & vbcrlf
         Next i
         MsgBox stest
    
    End Sub

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