|
-
Aug 28th, 2001, 09:39 AM
#1
Thread Starter
Fanatic Member
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.
-
Aug 28th, 2001, 10:15 AM
#2
Frenzied Member
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
-
Aug 28th, 2001, 10:32 AM
#3
Fanatic Member
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.
-
Aug 28th, 2001, 10:35 AM
#4
Frenzied Member
It worked when I had only 1 listbox on the form. Hmmm...
Let's see what happens when Ace tests it.
-
Aug 28th, 2001, 10:53 AM
#5
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:
Private Sub List1_GotFocus()
Dim ctrl As Control
For Each ctrl In Controls
If Not TypeOf ctrl Is Label And _
Not TypeOf ctrl Is Line And _
Not TypeOf ctrl Is Image And _
Not TypeOf ctrl Is Shape Then
ctrl.TabStop = False
End If
Next
End Sub
Private Sub List1_LostFocus()
Dim ctrl As Control
For Each ctrl In Controls
If Not TypeOf ctrl Is Label And _
Not TypeOf ctrl Is Line And _
Not TypeOf ctrl Is Image And _
Not TypeOf ctrl Is Shape Then
ctrl.TabStop = True
End If
Next
End Sub
Notice that I have a check for Label, Line, Image and Shape. This is because these controls dont have TabStop property.
-
Aug 28th, 2001, 11:43 AM
#6
Fanatic Member
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.
-
Aug 28th, 2001, 01:38 PM
#7
Thread Starter
Fanatic Member
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.
-
Aug 28th, 2001, 02:06 PM
#8
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.
-
Aug 28th, 2001, 02:22 PM
#9
Patoooey, this solution will be ideal for any Grid control where you would want to navigate with tab through cells.
-
Aug 28th, 2001, 02:50 PM
#10
Thread Starter
Fanatic Member
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
-
Aug 28th, 2001, 03:06 PM
#11
Fanatic Member
Change If to check for List.ListCount to -2.
VB Code:
Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyTab Then
If List1.ListIndex = List1.ListCount - 2 Then
List1.ListIndex = 0
Else
List1.ListIndex = List1.ListIndex + 2
End If
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|