December 5, 1995
This article explains how to design your Microsoft® Visual Basic® application so that the user can search for an item in a Combo Box control.
Microsoft® Visual Basic® Combo Box and List Box controls let you display a list of related items. For example, you might have a Combo Box control that displays a list of colors to the user. The user can select any color he or she wants to use by clicking that particular item in the Combo Box control.
When your Combo Box or List Box control contains a large number of items, it may be time-consuming to scroll through the items. An alternative solution to this problem can be found by allowing your user to type the item he or she wants to find. Then you can search through each entry in the control until you find the target item.
The Microsoft Windows® application programming interface (API) SendMessage function allows you to send a specific message to a window. In the example program below, a CB_FINDSTRING message is sent to the Combo Box control. This message tells Windows to search the Combo Box control for the value specified in the lParam variable.
The lParam variable must be set to the text string you want to locate in the Combo Box control. When the CB_FINDSTRING message is sent to the Combo Box control, the message will return the index number of the item that actually exists in the control. You must be aware, however, that the index number returned is zero-based. Therefore, if a value of 5 is returned, this actually corresponds to the fourth entry in the Combo Box control.
This program shows how to find an individual item in a Combo Box control.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
As Long
Const CB_FINDSTRING = &H14C
Private Sub Form_Load()
For X = 1 To 10
Combo1.AddItem "Item" & X
Next
End Sub
Private Sub Command1_Click()
X = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal "Item5")
MsgBox "Index number for this entry is: " & Str$(X)
End Sub
Run the example program by pressing f5. Click the Command Button control to search the Combo Box control for the item "Item5". A message box appears that identifies the index number (in this case, a value of 4) that corresponds to the item that was found in the Combo Box control. A value of 4 is returned because index numbers begin at zero.