cycling listbox items

iamwoturnot

New member
Joined
Aug 12, 2010
Messages
3
Programming Experience
5-10
Is it possible to time the interval between items in a listbox in a for loop? I have a for loop to loop through items in a listbox, and I want to set a timed interval between each item in the loop and run a command after each one, but when I try to do this with a timer, the set interval ticks off and then the loop is executed really fast. The only thing I have gotten to work is using sleep, which works between each item, but that freezes the form and that isn't what I want.

I want a set interval between each item within the loop. How do I do this if it is possible? Here is my loop currently using the sleep function. I don't have any examples of my attempts to use the timer because after several tries, I have given up and this what I currently have written:

VB.NET:
For i As Integer = 0 To FormMain.ListBox1.Items.Count - 1
                listitem = CStr(FormMain.ListBox1.Items(i))
                System.Threading.Thread.Sleep(5000)
                execute code for item here with a sub using listitem variable
Next i

This is the only thing I have gotten to work where there is a 5 second interval between each item and my command runs, but the form is unresponsive during the loop. I know this isn't the correct approach, but I have not been able to make it do like I want with the timer. Should I be using a For Each instead? Any help or advise would be greatly appreciated. Thanks in advance.
 
Last edited:
Declare the index variable outside the timer Tick handler, and set it before you start the timer. When timer ticks process current index then increment the index. When index reaches the number items in listbox you can stop the timer.
 
JohnH, thank you for your advice. I apologize for not getting back to you sooner but I have been a bit busy.
I took your advice and tinkered a bit while studying other examples, and I finally got what I wanted but I eliminated the For loop.
Let me show you how I got it to work. First, I declared two variables at the top of my code:

VB.NET:
Dim listitem As String
Dim curnum As Integer

Then on Button1_Click I called my Sub:

VB.NET:
Private Sub cycle()

        Call cyclerate() 'getting the rate variable

        Dim month As Long
        month = 18144000000

        If rate = "Hourly" Then
            Timer1.Interval = 3600000
            Timer1.Start()
            listitem = CStr(FormMain.ListBox1.Items.Item(curnum))
            curnum += 1
        ElseIf rate = "Daily" Then
            Timer1.Interval = 86400000
            Timer1.Start()
            listitem = CStr(FormMain.ListBox1.Items.Item(curnum))
            curnum += 1
        ElseIf rate = "Weekly" Then
            Timer1.Interval = 604600000
            Timer1.Start()
            listitem = CStr(FormMain.ListBox1.Items.Item(curnum))
            curnum += 1
        ElseIf rate = "Monthly" Then
            Timer1.Interval = month
            Timer1.Start()
            listitem = CStr(FormMain.ListBox1.Items.Item(curnum))
            curnum += 1
        End If

    End Sub

Then I put this in the Timer1_Tick event:

VB.NET:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

        If curnum = FormMain.ListBox1.Items.Count Then
            curnum = 0
        Else
            listitem = CStr(FormMain.ListBox1.Items.Item(curnum))
            curnum += 1
            code for item executed here
        End If

    End Sub

Then to stop the cycle with Button2, I used Timer1.Stop()

This is cycling the items just as I had wanted and executing specific code for each item at the interval set. When it reaches the end of the list, it starts over until Button2 is clicked to stop the procedure, so I am basically satisfied, because this is what I wanted (a continuous cycle with a set interval between each item until it was told to stop)

Thank you for your help.
 
Back
Top