Home > How-To Library > Controls

How to search a List Box for a specific piece of text using the SendMessage API. The text is highlighted as the user types in a textbox.

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Sub txtSearch_Change() Dim TempString As String Dim TheHandle As Long Dim Position As Long TempString = txtSearch.Text TheHandle = lstBox.hwnd If Len(TempString) = 0 Then 'search box is empty lstBox.ListIndex = 0 Else 'set list index to matching entry or back to first if nothing found Position = SendMessage(TheHandle, LB_FINDSTRING, -1, TempString) If Position = -1 Then lstBox.ListIndex = 0 Else lstBox.ListIndex = Position End If End If End Sub

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.