Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

list box selection, keyboard controls

Posted on 2004-09-12
8
Medium Priority
?
242 Views
Last Modified: 2012-05-05
I have a group of list boxes, for each listbox I have the following situation,

If I use the UP DOWN arrow keys to move up and down the list box, every item is automatically selected via  _Click() event. If I hold down the  arrow key  all the list is automatically selected, thats no good.

What I want is to use the UP DOWN arrow keys to highlight an item in the listbox and use the ENTER  key to select the item (if not possible the SPACE BAR), use LEFT RIGHT keys to navigate between the list boxes as the TAB key would do.

Is this possible.
0
Comment
Question by:dearness
  • 4
  • 3
8 Comments
 
LVL 1

Expert Comment

by:InVisal
ID: 12037477
'' // Create List(0) List(1) List(2)
'' // Here the code
'' // Is That What you want ?

Private Sub Form_Load()

List(0).AddItem "hello"
List(0).AddItem "hello1"
List(0).AddItem "hello2"

List(1).AddItem "hello"
List(1).AddItem "hello1"
List(1).AddItem "hello2"

List(2).AddItem "hello"
List(2).AddItem "hello1"
List(2).AddItem "hello2"

End Sub


Private Sub List_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)

    Dim i As Integer
    If KeyCode = 37 Then
        i = Index - 1
        If i < List.LBound Then i = List.UBound
        List(i).SetFocus
        Exit Sub
    End If
   
    If KeyCode = 39 Then
        i = Index + 1
        If i > List.UBound Then Index = List.LBound
        List(i).SetFocus
        Exit Sub
    End If

End Sub
0
 

Author Comment

by:dearness
ID: 12037525
The Right/Left arrow keys between the listboxes works good, but the Up/Down keys still select the list item automatically with out an Enter key ( Or SPACE BAR). I am using List(0).text as the selection, is this wrong?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12038563
Here you go.  Make sure the MultiSelect property of your ListBoxes is set to None (0).

Regards,

Idle_Mind

Option Explicit

Private ignore As Boolean

Private Sub Form_Load()
    List1(0).AddItem "cat"
    List1(0).AddItem "dog"
    List1(0).AddItem "fish"

    List1(1).AddItem "red"
    List1(1).AddItem "green"
    List1(1).AddItem "blue"

    List1(2).AddItem "cold"
    List1(2).AddItem "warm"
    List1(2).AddItem "hot"
   
    ignore = False
End Sub

Private Sub List1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyLeft
            KeyCode = 0
            Index = Index - 1
            If Index < List1.Lbound Then
                Index = List1.UBound
            End If
            List1(Index).SetFocus
                       
        Case vbKeyRight
            KeyCode = 0
            Index = Index + 1
            If Index > List1.UBound Then
                Index = List1.Lbound
            End If
            List1(Index).SetFocus
           
        Case vbKeyUp, vbKeyDown
            ignore = True
           
        Case vbKeySpace, vbKeyReturn
            List1_Click Index
           
    End Select
End Sub

Private Sub List1_Click(Index As Integer)
    If Not ignore Then
        If List1(Index).ListIndex <> -1 Then
            Debug.Print List1.Item(Index)
        End If
    End If
    ignore = False
End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dearness
ID: 12039390
Idle_Mind,
Thanks great answere, I can use this.
0
 

Author Comment

by:dearness
ID: 12039765
Idle_Mind
Just one more thing if you dont mind. As I Left or right arrow to a new list box, can it be easily acheived to remove the highlighting from the list box that lost focus
Thanks
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12039872
Sure:

        Case vbKeyLeft
            KeyCode = 0
            List1(Index).ListIndex = -1 '      <--- Unselect item in current ListBox
            Index = Index - 1
            If Index < List1.Lbound Then
                Index = List1.UBound
            End If
            List1(Index).SetFocus
                       
        Case vbKeyRight
            KeyCode = 0
            List1(Index).ListIndex = -1  '      <--- Unselect item in current ListBox
            Index = Index + 1
            If Index > List1.UBound Then
                Index = List1.Lbound
            End If
            List1(Index).SetFocus
0
 

Author Comment

by:dearness
ID: 12040101
Idle Mind,
Thanks alot for your help, I can carry on with the project from here.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12040116
No problem!

Regards,

{pseudocode} / {Time} ± ¼*ƒ(Me.Thoughts÷3)^² = Idle »(°_°)« Mind
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question