how to AutoScroll Listbox in continuous manner in VB.net?
ex AutoScroll
1
3
5
then start again from
1
3
5
You can set the index of the topmost entry with the TopIndex property. If you want to scroll automatically you could use a timer and modify the value of TopIndex on each timer event.
It sounds to me that you want the selection to loop from one end to the other when a person is scrolling with the arrow keys. This can be done with a combination of handling both the keydown and the keyup events and using a class level boolean:
Private Sub ListBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyUp
If e.KeyCode = Keys.Down AndAlso ListBox1.SelectedIndex = ListBox1.Items.Count - 1 AndAlso Not keytrap Then
ListBox1.SelectedIndex = 0
keytrap = True
End If
If e.KeyCode = Keys.Up And ListBox1.SelectedIndex = 0 AndAlso Not keytrap Then
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
keytrap = True
End If
End Sub
'This is needed to avoid the keyup event handler changing the index when the selection moves to the end from the second to the end.
Dim keytrap As Boolean = False
Private Sub ListBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyDown
If e.KeyCode = Keys.Down Then
If ListBox1.SelectedIndex = ListBox1.Items.Count - 2 Then
keytrap = True
ElseIf ListBox1.SelectedIndex = ListBox1.Items.Count - 1 Then
keytrap = False
End If
End If
If e.KeyCode = Keys.Up Then
If ListBox1.SelectedIndex = 1 Then
keytrap = True
ElseIf ListBox1.SelectedIndex = 0 Then
keytrap = False
End If
End If
End Sub
When the selection is at the top or the bottom pressing the appropriate arrow will move the selection to the other end of the list.
thnk, but want to Scroll Listbox item in continuous manner with timer and button
tinstaa ? , the only person on this site who post ex. keep up the work.
What you probably want is, in the handler for the timer tick event, to increment the selectedindex of the listbox and a button to toggle the timer off and on. when the index reaches listbox.items.count, which is one past the highest index, reset the index to 0. One way to do that is to use the Mod
operator. So that:
ListBox1.SelectedIndex = (ListBox1.SelectedIndex + 1) Mod ListBox1.Item.Count
This will automatically loop around to 0 when it reaches the top index.
This also has the advantage that if you edit the items the code will react to the new count.
Create a listbox, ListBox1, and a timer, Timer1.
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim numitems As Integer = ListBox1.Height \ ListBox1.ItemHeight
If ListBox1.TopIndex < ListBox1.Items.Count - numitems Then
ListBox1.TopIndex += 1
Else
ListBox1.TopIndex = 0
End If
End Sub
End Class
By calculating and using numitems (the number of displayed lines) you can reset to the top of the list after the last item is displayed.
Just for completeness:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
ListBox1.SelectedIndex = (ListBox1.SelectedIndex + 1) Mod ListBox1.Item.Count
End Sub
Now you have 2 methods to choose from, the displayed items changing 1 item at a time and starting over when you reach the end or selecting each item in turn and looping to the top when it gets to the end.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.