Results 1 to 3 of 3

Thread: listbox movenext?

  1. #1
    New Member
    Join Date
    Sep 12
    Posts
    3

    listbox movenext?

    i am currently working on how to
    movenext listbox or auto movenext

    but after i execute it
    it goes on the last record not on the next record

    if i change the listcount to 1 a errors comes up, if i change the listcount to + 1 nothing happens...
    i dont know why it was always goes on the last even i put + 1?

    can anyone help me with this

    Code:
    Dim i As Integer
    For i = 0 To List1.ListCount - 1
    i = i + 1
    List1.Selected(i) = True
    Text4.Text = List1.Text
    Next

  2. #2
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,150

    Re: listbox movenext?

    You're manually changing the loop control variable (i) within the loop.
    You need to remove the 'i = i + 1' statement
    EDIT: Actually, what exactly are you trying to do ? By using a loop you'll always end up with the last item in the ListBox

    EDIT: This code will move through the listbox items one at a time by clicking on Command2 button. When it reaches the end it starts over at the first item.
    Code:
    Private Function ListMoveNext(lst As ListBox, Optional boReStart As Boolean = False) As String
    Static lngPos As Long
    If boReStart Then lngPos = 0
    If lngPos > lst.ListCount - 1 Then
        ListMoveNext = vbNullString
    Else
        lst.Selected(lngPos) = True
        ListMoveNext = lst.List(lngPos)
        lngPos = lngPos + 1
    End If
    End Function
    
    Private Sub Command2_Click()
    Dim strItem As String
    strItem = ListMoveNext(lst)
    If strItem <> vbNullString Then
        Text4.Text = strItem
    Else
        Text4.Text = ListMoveNext(lst, True)
    End If
    End Sub
    Last edited by Doogle; Sep 10th, 2012 at 03:38 AM.

  3. #3
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    528

    Re: listbox movenext?

    Doesn't the Listbox support the For-Each Statement?

Posting Permissions

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