Solved

list box selection, keyboard controls

Posted on 2004-09-12
8
234 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input past end of file vbs script 9 92
Vb.net 2008 2 59
Run code from text file in vb 1 64
How to incorporate an error traping routing to existing code in VB6 11 39
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

785 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