Solved

Type ahead function for listbox in vb6

Posted on 2004-08-20
5
383 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now