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

  • This is the source link

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

    http://support.microsoft.com/kb/KbView/141027

    Reply
  • 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
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

  • This report outlines the future look of Forrester's solution for security and risk (S&R) executives working on building an identity and access management strategy for the extended enterprise. We designed this report to help you understand and navigate the major business and IT trends affecting identity and access management (IAM) during the next five years. IAM in 2012 has become a tool not just for security but also for business agility. Competitive challenges push businesses into the cloud and encourage …

Most Popular Programming Stories

More for Developers

RSS Feeds

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