Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSelect a ComboBox value based on text the user types
KeywordsComboBox, type
CategoriesControls
 
In the ComboBox's KeyPress event hander, keep track of the keys the user has pressed. When you see a Backspace, remove the last character. When you see a Delete, clear the string. Add any printing character to the string.

After you have the new string, search the ComboBox's list of values for it.

 
Private Sub cboNames_KeyPress(KeyAscii As Integer)
Static target As String
Dim ch As String

    If KeyAscii = vbKeyEscape Then
        ' Escape. Clear the string.
        target = ""
        cboNames.ListIndex = -1
    ElseIf KeyAscii = vbKeyBack Then
        ' Backspace. Delete a character.
        If Len(target) > 0 Then
            target = Left$(target, Len(target) - 1)
        End If
        SelectComboString cboNames, target
    Else
        ' See if the character is visible.
        ch = Chr$(KeyAscii)
        If ch >= " " And ch <= "~" Then
            ' Add this character to the string.
            target = target & Chr$(KeyAscii)

            SelectComboString cboNames, target
        End If
    End If

    ' Make the ComboBox ignore the character.
    KeyAscii = 0
End Sub
 
You might want to modify the code to reset the target string to "" when the control gains focus, or to set target to the value selected if the user selects an entry using the mouse.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated