Listbox and scrolling with arrow keys

  • Thread starter Thread starter gv
  • Start date Start date
G

gv

Hi all,

Using VB 2005 Express

When pressing the down key in a listbox and getting to the last item I want
to
move right back to the top and vise versa so it never stops when pressing
the
arrow keys. The code below moves it when it gets to the end of the list biut
skips the first item
and moves to index 1.

Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown

If ListBox1.SelectedIndex = (ListBox1.Items.Count - 1) And e.KeyCode =
Keys.Down Then

ListBox1.SetSelected(0, True)

End If

End Sub

thanks
Gerry
 
gv said:
Hi all,

Using VB 2005 Express

When pressing the down key in a listbox and getting to the last item I want
to
move right back to the top and vise versa so it never stops when pressing
the
arrow keys. The code below moves it when it gets to the end of the list biut
skips the first item
and moves to index 1.

Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown

If ListBox1.SelectedIndex = (ListBox1.Items.Count - 1) And e.KeyCode =
Keys.Down Then

ListBox1.SetSelected(0, True)

Here add

e.Handled = True
End If

End Sub

This will prevent the normal keydown operation happening, which is what
is moving the selection from 0 to 1 after you have set it.
 
Thank You!!!!!
Gerry

Larry Lard said:
Here add

e.Handled = True


This will prevent the normal keydown operation happening, which is what
is moving the selection from 0 to 1 after you have set it.
 
Back
Top