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

Const LB_FINDSTRING = &H18F

private Sub Form_Load()

    With List1
        .Clear
        .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



Comments

  • 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

    Reply
  • 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.

    Reply
  • 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

    Reply
  • 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!

    Reply
  • 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?

    Reply
  • 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

    Reply
  • Thanx thanx thanx

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

    Originally posted by: xenocide

    Ive needed that forever!

    Reply
  • 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.

    Reply
  • Search with the help of textbox and listbox

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

    Originally posted by: Anil

    good.

    Reply
  • GOD BLESS YOU!!!

    Posted by Legacy on 05/26/2002 12:00am

    Originally posted by: zerglingcho

    That's exactly what i was looking for for about 2 months !

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds