Solved

Type ahead function for listbox in vb6

Posted on 2004-08-20
5
412 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
[X]
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
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

617 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