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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds