When a List Box control has the focus, it will automatically scroll to the first item that begins with the letter you type. It will not, however, automatically select items based on more than the first letter. For example, if you type go, you may want the control to select the first item that begins with go. This tip explains how you can implement this behaviour using the API function SendMessage. Its declaration, which must be included in the program, is as follows:
You'll also need the following constant, which tells the List Box control to select the first item that begins with the specified prefix:
In addition to the List Box, this technique requires a Text Box control. The user enters text in the Text Box, and the List Box automatically selects the first matching item. The message is sent in the Text Box's Change event procedure:
Letting the user select List Box items based on more than just the first letter can be particularly useful when the list contains many items.
Code: VB
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As String) As Long
Code: VB
Public Const LB_SELECTSTRING = &H18C
Code: VB
Private Sub Text1_Change()
If Text1.Text <> "" Then
SendMessage List1.hwnd, LB_SELECTSTRING, -1, Text1.Text
End If
End Sub

