Home » 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.
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.
Posted on October 5, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | '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. 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 |
Enjoy this article?
Filed under: Controls
Leave a comment