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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • As a development and deployment platform, RHEL offers an efficient, scalable, and robust operating environment with certified security and flexible deployment options in physical and virtualized environments. To assess and quantify the business benefits of RHEL, IDC recently conducted in-depth interviews with IT staff members of 21 companies using RHEL servers. The organizations represent a broad range of industries and have an average of 22,700 employees. RHEL servers accounted for 23% of the servers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds