The information in this article applies to:
- Microsoft Visual Basic Professional and Enterprise Editions for
Windows, version 5.0
- Standard, Professional, and Enterprise Editions of Microsoft Visual
Basic, 16-bit and 32-bit, for Windows, version 4.0
SUMMARY
The ComboBox that ships with Visual Basic has simplistic functionality
whereas many third-party controls offer additional extra features. One of
these third-party features is the ability of the ComboBox to search for
text and find entries starting with that text as you type text into the
ComboBox.
For example, if you type the letters "He," the ComboBox will search for
the first text entry starting with "He," and will display the full text,
such as "Hello World."
Below is a code sample that demonstrates how to achieve this functionality
with the ComboBox that ships with Visual Basic.
MORE INFORMATION
Step by Step Example
- Start a new project in Visual Basic. Form1 is created by default.
- Place a ComboBox control on the form.
- Add the following code to the Form1 code window:
Option Explicit
Private Const WM_SETREDRAW = &HB
Private Const KEY_A = 65
Private Const KEY_Z = 90
#If Win32 Then
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Long) As Long
#Else
Private Declare Function SendMessage Lib "User" ( _
ByVal hwnd As Integer, ByVal wMsg As Integer, _
ByVal wParam As Integer, lParam As Any) As Long
#End If
Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim sComboText As String
Dim iLoop As Integer
Dim sTempString As String
Dim lReturn As Long
If KeyCode >= KEY_A And KeyCode <= KEY_Z Then
'only look at letters A-Z
sTempString = Combo1.Text
If Len(sTempString) = 1 Then sComboText = sTempString
lReturn = SendMessage(Combo1.hWnd, WM_SETREDRAW, False, 0&)
For iLoop = 0 To (Combo1.ListCount - 1)
If UCase((sTempString & Mid$(Combo1.List(iLoop), _
Len(sTempString) + 1))) = UCase(Combo1.List(iLoop)) Then
Combo1.ListIndex = iLoop
Combo1.Text = Combo1.List(iLoop)
Combo1.SelStart = Len(sTempString)
Combo1.SelLength = Len(Combo1.Text) - (Len(sTempString))
sComboText = sComboText & Mid$(sTempString, Len(sComboText) + 1)
Exit For
Else
If InStr(UCase(sTempString), UCase(sComboText)) Then
sComboText = sComboText & Mid$(sTempString, Len(sComboText) _
+ 1)
Combo1.Text = sComboText
Combo1.SelStart = Len(Combo1.Text)
Else
sComboText = sTempString
End If
End If
Next iLoop
lReturn = SendMessage(Combo1.hWnd, _
WM_SETREDRAW, True, 0&)
End If
End Sub
Sub Form_load()
Combo1.AddItem "Alpha"
Combo1.AddItem "Beta"
Combo1.AddItem "Charlie"
Combo1.AddItem "Delta"
Combo1.AddItem "Dingo"
End Sub
- On the Run menu, click Start, or press the F5 key to run the program.
If you type the letter "D," you will notice that "Delta" is the active text
in the ComboBox. If you then type an "I," the text "Dingo" will be
displayed. With all subsequent letters you enter, the next word in the
ComboBox list will begin with the sequence of characters you have typed.
NOTE: The style property of the ComboBox must be "0 - Dropdown Combo" for
this to work correctly.
Keywords : GnrlVb kbprg
Technology : kbvba
Version : WINDOWS:4.0 5.0
Platform : WINDOWS
Issue type : kbhowto
|