Solved

list box selection, keyboard controls

Posted on 2004-09-12
8
232 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 85

Accepted Solution

by:
Mike Tomlinson earned 125 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
 

Author Comment

by:dearness
ID: 12039390
Idle_Mind,
Thanks great answere, I can use this.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 85

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 85

Expert Comment

by:Mike Tomlinson
ID: 12040116
No problem!

Regards,

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now