Solved

Type ahead function for listbox in vb6

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 70
Hide vba in gp 7 127
Visual Studio 2005 text editor 10 57
VBA to find and replace multiline text from VBA modules 8 108
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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

710 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