Ok i manage to autoselect items on listbox till reach the end of list, but i need it to automatically loop back to the fist selection of indexes when it reach the end list and so on .. i need help on this part newbie here :)

if Me.Listboxs1.SelectedIndex + 1  < Me.ListBox1.items.count then
    Me.ListBox1.SelectedIndex + = 1
End if

`

Dani AI

Generated

is right: his wrap logic will cycle forever if you run it from a timer. If what you really want is a back-and-forth (ping-pong) effect instead of jumping from the last item back to the first, keep a direction flag and flip it at the ends.

' Form-level field
Private movingDown As Boolean = True

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim n = ListBox1.Items.Count
    If n = 0 Then Exit Sub

    If ListBox1.SelectedIndex < 0 Then
        ListBox1.SelectedIndex = 0
        Exit Sub
    End If

    If movingDown AndAlso ListBox1.SelectedIndex >= n - 1 Then movingDown = False
    If Not movingDown AndAlso ListBox1.SelectedIndex <= 0 Then movingDown = True

    ListBox1.SelectedIndex += If(movingDown, 1, -1)
End Sub

If forward-only cycling is fine, a compact alternative to ’s If/Else is to use modulo arithmetic. It also handles the initial state when nothing is selected.

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim n = ListBox1.Items.Count
    If n = 0 Then Exit Sub
    Dim current = Math.Max(ListBox1.SelectedIndex, -1)
    ListBox1.SelectedIndex = (current + 1) Mod n
End Sub

Practical tips:

  • Make sure you are using System.Windows.Forms.Timer in WinForms so the tick runs on the UI thread. If you use System.Timers.Timer or System.Threading.Timer, update the ListBox via Me.Invoke(...) to avoid cross-thread exceptions.
  • Choose a sensible Timer.Interval (e.g., 150–500 ms) so the selection does not race past the user.
  • If this tag truly means ASP.NET, remember server-side SelectedIndex changes will not animate the browser. You would need client-side JavaScript (for example, setInterval) or an ASP.NET AJAX Timer in an UpdatePanel to visually advance the selection. , that distinction often trips people up.

Recommended Answers

All 4 Replies

If ListBox1.SelectedIndex = ListBox1.Items.Count - 1 Then
    ListBox1.SelectedIndex = 0
Else
    ListBox1.SelectedIndex += 1
End If

thanks Reverend Jim
Yes your code does select the first index after reach the end list.. but i want something that loops back and fort till i stop the timer
by the way thanks for the reply bro :)

Glad to help.

My code selects the next item in the list and wraps back to the beginning when it reaches the end of the list. If it is in a timer then it will cycle continuously. If there are three items in the list then SelectedIndex will cycle as 0, 1, 2, 0, 1, 2, ...

thanks again Reverend Jim i tested the code now and its working perfectly

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.