Make A Quick Search Combo Box

'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Add 1 Combo Box to your form. Set the Combo Box Style property to 2 - DropDown List.
'Add few items to the Combo Box list, some of them should begin with the same character.
'When you will press a key, the first item that begins with the key you pressed will be selected.
'If you will press the same key again, the second item that begins with the key you pressed
'will be selected.
'Insert the following code to the module :

Dim strCombo As String
Const WM_SETREDRAW = &HB
Const KEY_A = 65
Const KEY_Z = 90
Declare Function SendMessage Lib "User32" (ByVal hWnd As Integer, ByVal wMsg As _
Integer, ByVal wParam As Integer, lParam As Any) As Long

'Insert the following code to your form:

Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim x%
Dim strTemp$
Dim nRet&
If KeyCode >= KEY_A And KeyCode <= KEY_Z Then
'only look at letters A-Z
strTemp = Combo1.Text
If Len(strTemp) = 1 Then strCombo = strTemp
nRet& = SendMessage(Combo1.hWnd, WM_SETREDRAW, False, 0&)
For x = 0 To (Combo1.ListCount - 1)
If UCase((strTemp & Mid$(Combo1.List(x), Len(strTemp) + 1))) = UCase(Combo1.List(x)) Then
Combo1.ListIndex = x
Combo1.Text = Combo1.List(x)
Combo1.SelStart = Len(strTemp)
Combo1.SelLength = Len(Combo1.Text) - (Len(strTemp))
strCombo = strCombo & Mid$(strTemp, Len(strCombo) + 1)
Exit For
Else
If InStr(UCase(strTemp), UCase(strCombo)) Then
strCombo = strCombo & Mid$(strTemp, Len(strCombo) + 1)
Combo1.Text = strCombo
Combo1.SelStart = Len(Combo1.Text)
Else
strCombo = strTemp
End If
End If
Next
nRet& = SendMessage(Combo1.hWnd, WM_SETREDRAW, True, 0&)
End If
End Sub

Go Back