Results 1 to 11 of 11

Thread: Tab Through ListBox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2001
    Posts
    759

    Tab Through ListBox

    Hi Everyone,

    So far I have a ListBox set up like this that I would like to be able to tab through.

    Code:
    Ace Jones
         Bend, Oregon
    Elizabeth Jones
         San Diego, California
    What I would like to do is this. When the ListBox loads I want to have the first user name highlighted. in this case it would be 'Ace Jones', then when the user hits the tab key on the keyboard I want the second user name in the list highlighted, 'Elizabeth Jones' in this case. Any ideas on how to do this? Also, when a user name has been highlighted I would like to ba able to display other information about the user in some text boxes that changes as the users are highlighted. Any help would be greatly appreciated. Thanks.

  2. #2
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    Private Sub Form_Load()
    YourListBox.ListIndex = 0
    End Sub

    Private Sub lstYourListBox_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
    If lstYourListBox.ListIndex = lstYourListBox.ListCount - 1 Then
    lstYourListBox.ListIndex = 0
    Else
    lstYourListBox.ListIndex = lstYourListBox.ListIndex + 1
    End If
    End If
    End Sub

  3. #3
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Will that code work for a TAB ?? I thought the form took over keystrokes if there are more then one control with a TabIndex and you had to capture them in the form_keypress

    Why change the standard UI for scrolling thru a Listbox anyway. Your just going to confuse your users.

  4. #4
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    It worked when I had only 1 listbox on the form. Hmmm...
    Let's see what happens when Ace tests it.

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Yes, if you have more then one control on the form you will not get TAB key captured. BUT......there is a workaround. When you recieve a focus on the ListBox, you can disable all tabstops which will give you the ability to capture the TAB key again, then when you lose focus from the ListBox, restore all tabstops.
    VB Code:
    1. Private Sub List1_GotFocus()
    2.     Dim ctrl As Control
    3.    
    4.     For Each ctrl In Controls
    5.         If Not TypeOf ctrl Is Label And _
    6.                 Not TypeOf ctrl Is Line And _
    7.                 Not TypeOf ctrl Is Image And _
    8.                 Not TypeOf ctrl Is Shape Then
    9.             ctrl.TabStop = False
    10.         End If
    11.     Next
    12. End Sub
    13.  
    14. Private Sub List1_LostFocus()
    15.     Dim ctrl As Control
    16.    
    17.     For Each ctrl In Controls
    18.         If Not TypeOf ctrl Is Label And _
    19.                 Not TypeOf ctrl Is Line And _
    20.                 Not TypeOf ctrl Is Image And _
    21.                 Not TypeOf ctrl Is Shape Then
    22.             ctrl.TabStop = True
    23.         End If
    24.     Next
    25. End Sub
    Notice that I have a check for Label, Line, Image and Shape. This is because these controls dont have TabStop property.

  6. #6
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Great solution Serge. Tested and works perfect. Still not sure what this is really good for tho.
    Last edited by Patoooey; Aug 28th, 2001 at 11:46 AM.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2001
    Posts
    759
    Thanks for your responses everyone. I still cannot get this to work right though. When I try to use the Got focus and lost focus procedures I receive an error message saying that object doesn't support this property or method. The program crashes on the line ctrl.TabStop = Flase. The reason I am trying to run the program like this is because it is a search form where a user can look up other users. Multiple users may have identical names, therefore, I wanted to be able to just tab down the list instead of clicking on each user.

  8. #8
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    It means that some other controls you have on the form don't have TabStop. When you get the error, press Debug and type in the Immediate window ?ctrl.Name
    You should see what that control is. You can then add the check to the loop.

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Patoooey, this solution will be ideal for any Grid control where you would want to navigate with tab through cells.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2001
    Posts
    759
    Thanks Serge,

    I figured out what control was wrong and got that fixed. Now I have another problem, when a user tabs through a ListBox I need to program to skip the city, state line in the Listbox and just go to every user name (located on every other line, as shown in my previous post). When I change + 1 to + 2 in this line I get an error message after the program has gone through the List once: Invalid property value. Then the program crashes on the line listed below. Any ideas on how to fix this? Thanks again for your help.

    Code:
    lstYourListBox.ListIndex = lstYourListBox.ListIndex + 1

  11. #11
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Change If to check for List.ListCount to -2.

    VB Code:
    1. Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = vbKeyTab Then
    3.         If List1.ListIndex = List1.ListCount - 2 Then
    4.             List1.ListIndex = 0
    5.         Else
    6.             List1.ListIndex = List1.ListIndex + 2
    7.         End If
    8.     End If
    9. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width