Solved

Type ahead function for listbox in vb6

Posted on 2004-08-20
5
391 Views
Last Modified: 2013-12-25
Anyone know how to do a type ahead for a listbox in vb6?

ex. The listbox contains:

Acorn
Apple
Banana

When you type A then P it should change the focus to Apple.
0
Comment
Question by:n0ell
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:llcooljayce
ID: 11856058
0
 
LVL 5

Accepted Solution

by:
gary_j earned 250 total points
ID: 11856210
I add a "navigator" text box, then do this in the textbox change event:

dim i as integer

if list1.listcount > 0 then
     for i = 0 to list1.listcount - 1
           if left(list1.list(i),len(trim(text1.text))) = trim(text1.text) then
                 list1.listindex = i
                 exit for
           endif
     next 'i
endif
0
 

Author Comment

by:n0ell
ID: 11869938
Hi gary j,

Thanks for responding to my question. I tried to implement this code and it worked when i type on the navigator text box however i want to implement this using only the Listbox so that when i type on it, it searches for a match in my list. What i did was to hide the navigator text box and then passed the search criteria using the keypressed event of my listbox. For some reason that i still have not figured out yet, it jumps an extra line after it finds a match in my list. e.g.

abc
abcappend
acc

When I use the text "abca" as my search criteria, the application is able to find abcappend but then jumps to the next one (acc)

here are the code that i used :
'code for my listbox
Private Sub lstMed_KeyPress(KeyAscii As Integer)
   If KeyAscii = 13 Then
        Text1.Text = ""
    ElseIf KeyAscii = 8 Then
        If Text1.Text <> "" Then
            Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
        End If
    Else
         Text1.Text = Text1.Text & Chr(KeyAscii)
    End If
End Sub
 
'code for hidden navigator textbox
Private Sub Text1_Change()
If lstMed.ListCount > 0 Then
     For i = 0 To lstMed.ListCount - 1
           If Left(lstMed.List(i), Len(Trim(Text1.Text))) = Trim(Text1.Text) Then
                 lstMed.ListIndex = i
                 Exit For
           End If
     Next 'i
End If
End Sub
 
Thanks.
0
 
LVL 5

Expert Comment

by:gary_j
ID: 11870213
I tested this and I think it's because your lstMed_KeyPress event is still in "control" -- in other words, with the keystrokes you're making, the normal operation of that event will move focus to index 2.  That normal operation as you leave the keypress event is over-riding what you're doing in the text1 change event.  
0
 

Author Comment

by:n0ell
ID: 11870585
Gary J,

Thanks for the speedy reply. What I did was to put the text1_change code and put them in the lstMed_KeyPress function and just set the keyascii = 0 so that it will not override the the setting of the list index. It's now working.

Here's the update code:
Public Searchkey as string

Private Sub lstMed_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
        searchkey = ""
    ElseIf KeyAscii = 8 Then
        If searchkey <> "" Then
            searchkey = Left(searchkey, Len(searchkey) - 1)
        End If
    Else
         searchkey = searchkey & Chr(KeyAscii)
    End If


    Dim i As Integer
   
    If lstMed.ListCount > 0 Then
         For i = 0 To lstMed.ListCount - 1
               If Left(lstMed.List(i), Len(Trim(searchkey))) = Trim(searchkey) Then
                     lstMed.ListIndex = i
                     Exit For
               End If
         Next 'i
    End If
    KeyAscii = 0
End Sub

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

813 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

11 Experts available now in Live!

Get 1:1 Help Now