Search Listboxes as you Type

Search as you type

By changing the SendMessage Function's "ByVal wParam as Long" to "ByVal wParam as String", we change the search ability from first letter only, to "change-as-we-type" searching.

Here's some example code. Start a new Standard EXE project and add a ListBox (List1) and a TextBox (Text1), then paste in the following code :

option Explicit

'Start a new Standard-EXE project.
'Add a textbox and a listbox control to form 1
'Add the following code to form1:

private Declare Function SendMessage Lib "User32" _
        Alias "SendMessageA" (byval _
        hWnd as Long, _
        byval wMsg as Integer, _
        byval wParam as string, _
        lParam as Any) as Long


private Sub Form_Load()

    With List1
        .AddItem "RAM"
        .AddItem "rams"
        .AddItem "RAMBO"
        .AddItem "ROM"
        .AddItem "Roma"
        .AddItem "Rome"
        .AddItem "Rommel"
        .AddItem "Cache"
        .AddItem "Cash"
    End With

End Sub

private Sub Text1_Change()
    List1.ListIndex = SendMessage(List1.hWnd, LB_FINDSTRING, _
                      Text1, byval Text1.Text)
End Sub

Download zipped project file (2k)

Screen shot


  • This is the source link

    Posted by duplicate on 02/12/2015 11:59am

  • Same in MS Access VB

    Posted by shameemaj2 on 01/08/2007 10:53am

    Hi i found ur coding for a searchable listbox very useful. Still, I am working in MS Access' VB and i guess whether you could give me the corresponding replacement for the "listobx.hWnd" which is giving me the following Compile Error: Method or Data Member not found. I wish u could help...? Many thanks

  • Having some trouble

    Posted by jackie314 on 12/26/2005 07:44pm

    I am having some trouble with this code. When I run it in VB.NET 2005 it gives me a PInvokeStackImbalance error message. There are some items that have been changed in order for me to run the code at all. I have been searching for this kind of function for awhile and I am very new to programming and do not know anything about handles below are the changes that I had to make to the code, any help on this would be greatly appreciated: Private Declare Function SendMessage Lib "User32" _ Alias "SendMessageA" (ByVal _ hWnd As Long, _ ByVal wMsg As Integer, _ ByVal wParam As String, _ ByVal lParam As String) As Long (VB.NET won't accept As Any in a declaration statement) Dim TheListBoxHandle As Integer TheListBoxHandle = lstSearchArea.Handle.ToInt64 lstSearchArea.SelectedIndex = SendMessage(TheListBoxHandle, LB_FINDSTRING, _ txtSearch.Text, txtSearch.Text) ListIndex is not a function of the list box anymore, and it won't accept txtSearch as a valid string.

  • Thank You Really

    Posted by Legacy on 09/03/2003 12:00am

    Originally posted by: Param

    Thank u. This is the thing I was searching for .
    Thanx again

  • ListSearch

    Posted by Legacy on 09/01/2003 12:00am

    Originally posted by: Sameer Kulkarni

    Thanks a lot i think its exactly what i really want!

  • Problem in VBA

    Posted by Legacy on 07/18/2003 12:00am

    Originally posted by: Tony Pinto

    Have tried this code in VBA under Access2000.
    On compiling the argument "ListView.hWnd" shows error "member not found". Is there a reason why VB code will not run under VBA?

  • Search as you type

    Posted by Legacy on 09/23/2002 12:00am

    Originally posted by: Tony

    Really cool! That is what I am trying to looking for

  • Thanx thanx thanx

    Posted by Legacy on 08/15/2002 12:00am

    Originally posted by: xenocide

    Ive needed that forever!

  • ListIndex is read-only

    Posted by Legacy on 08/02/2002 12:00am

    Originally posted by: Myron Marston

    Looks like a very useful piece of code. However, I have found that ListIndex is a read-only property in Access 2000 VBA.

  • Search with the help of textbox and listbox

    Posted by Legacy on 07/11/2002 12:00am

    Originally posted by: Anil


  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date