Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

moving through a listbox

Status
Not open for further replies.

MBall2003

Programmer
Joined
May 30, 2003
Messages
61
Location
US
is there any way to move through a list box such that when your at index o a keypress up will bring you to the last index and vice versa if you're at the last record and a key press down will bring you to index 0 and help would be a ppreciated thanks

mball
 
Use the ListBox's KeyDown event to test for the up/down arrow keys. The box's ListIndex property tells you the currently selected row, so your code would be something like this:
Code:
    With ListBox1
        If KeyCode = vbKeyUp And .ListIndex = 0 Then
            .ListIndex = .ListCount - 1
            KeyCode = 0
        ElseIf KeyCode = vbKeyDown And .ListIndex = .ListCount - 1 Then
            .ListIndex = 0
            KeyCode = 0
        End If
    End With

Note: If you're going to process the keystroke instead of letting the list box do it, you need to "eat" the keystroke by setting KeyCode to 0. Otherwise, the list box will process it again after you, which will result in double movement.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
This is a start. It should take you through the list box items. Use the wiz to choose one instead of ItemsSelected


Dim ctl As Control
Dim varItm As Variant

Set ctl = Me!MyList
For Each varItm In ctl.ItemsSelected
MsgBox ctl.ItemData(varItm)
Next

rollie@bwsys.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top