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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read