-
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...:confused:
i dont know why it was always goes on the last even i put + 1?
can anyone help me with this :sick:
Code:
Dim i As Integer
For i = 0 To List1.ListCount - 1
i = i + 1
List1.Selected(i) = True
Text4.Text = List1.Text
Next
-
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
-
Re: listbox movenext?
Doesn't the Listbox support the For-Each Statement?