Question Help with a simple loop

gvjonnyv

Member
Joined
Jan 15, 2009
Messages
7
Programming Experience
Beginner
I'm trying to create new outputs from previously outputted strings in a list box. I thought I'd run a loop by selected index value to get the program to run through each item in the list box (in this case, 4 items). Instead of running through each item in the list it runs through the first item 4 times. What am I doing wrong???
VB.NET:
        Dim i As Integer = 0
        Do Until i = 3
            ListBox1.SelectedIndex = i
            SearchDirectory(ListBox1.SelectedItem)
            i += 1
        Loop
or
VB.NET:
        Dim i As Integer = 0
        For i = 0 To 3
            ListBox1.SelectedIndex = i
            SearchDirectory(ListBox1.SelectedItem)
        Next i

also, if i want to run this loop until the indexvalue stops, how could i go about doing that???
 
listbox.clearSelected

ok, i found out why it wasn't working... the loop works fine i just need to clear my selection in the list box so it doesn't choose the first selection.

I could still use some help as to how to get it to stop when the list box runs out of items.
 
So you want a loop to stop at the 4th item or stop when it runs out of items (if less than 4) correct?

Assuming that's true, I would use an integer for the end of the loop:
VB.NET:
Dim EndLoopNum As Integer
If ListBox1.Items.Count > 4I Then EndLoopNum = 4I Else EndLoopNum = ListBox1.Items.Count - 1I

For i = 0 To EndLoopNum
    SearchDirectory(ListBox1.Items(i))
Next i
 
Great, thanks a lot! It was driving me nuts that I couldn't figure out something simple like how to pick the last item in the list box.
 
Back
Top