Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

list box selection, keyboard controls

Posted on 2004-09-12
8
237 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

838 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