CodeItBetter Programming Another VB Programming Blog

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
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


ERROR: That reCAPTCHA response was incorrect.

 

No trackbacks yet.