Solved

Type ahead function for listbox in vb6

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 132
Passing a Text Box name to a Sub 6 99
Zip Folders Using Chilkat Routines 1 58
MS SQL Update query with connected table data 3 41
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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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…

830 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