Solved

list box selection, keyboard controls

Posted on 2004-09-12
8
231 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now