VB icon

Autocomplete for winform combo box

Email
Submitted on: 1/1/2015 12:25:00 PM
By: Steve Briski (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: VB.NET
Views: 3000
 
     Autocomplete for winforms combo boxes. This was found on MSDN and enhanced to prevent the user from entering a selection that isn't in the combo box list. The code finds and selects the first match for the text that the user entered--if no match is found it selects the first entry that is "next" after the entered text. The delete and backspace key are also handled.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Autocomplete for winform combo box
// Description:Autocomplete for winforms combo boxes. This was found on MSDN and enhanced to prevent the user from entering a selection that isn't in the combo box list. The code finds and selects the first match for the text that the user entered--if no match is found it selects the first entry that is "next" after the entered text. The delete and backspace key are also handled.
// By: Steve Briski (from psc cd)
//
// Inputs:the name of the combo box and the event arg for the combo box
//
// Returns:nothing
//
// Assumes:Add the module to your application, then from your form just call the AutoComplete function passing in the name of your combobox and the event arg.
//**************************************

Module Module1
Public Sub AutoComplete(ByVal cbo As ComboBox, ByVal e As System.Windows.Forms.KeyEventArgs)
'call this from your form passing in the name of your combobox and the event arg:
'AutoComplete(cboState, e)
Dim iIndex As Integer
Dim sActual As String
Dim sFound As String
Dim bMatchFound As Boolean
'if backspace then remove the last character that was typed in and try to find a match.
'note that the selected text from the last character typed in to the end of the combo text field will also be deleted.
If e.KeyCode = Keys.Back Then
cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
End If
' Do nothing for some keys such as navigation keys.
If ((e.KeyCode = Keys.Left) Or _
(e.KeyCode = Keys.Right) Or _
(e.KeyCode = Keys.Up) Or _
(e.KeyCode = Keys.Down) Or _
(e.KeyCode = Keys.PageUp) Or _
(e.KeyCode = Keys.PageDown) Or _
(e.KeyCode = Keys.Home) Or _
(e.KeyCode = Keys.End)) Then
Return
End If
Do
' Store the actual text that has been typed.
sActual = cbo.Text
' Find the first match for the typed value.
iIndex = cbo.FindString(sActual)
' Get the text of the first match.
'if index > -1 then a match was found.
If (iIndex > -1) Then
sFound = cbo.Items(iIndex).ToString()
' Select this item from the list.
cbo.SelectedIndex = iIndex
' Select the portion of the text that was automatically
' added so that additional typing will replace it.
cbo.SelectionStart = sActual.Length
cbo.SelectionLength = sFound.Length
bMatchFound = True
Else
'if there isn't a match and the text typed in is only 1 character or nothing then just select the first
'entry in the combo box.
If sActual.Length = 1 Or sActual.Length = 0 Then
cbo.SelectedIndex = 0
cbo.SelectionStart = 0
cbo.SelectionLength = Len(cbo.Text)
bMatchFound = True
Else
'if there isn't a match for the text typed in then remove the last character of the text typed in
'and try to find a match.
cbo.SelectionStart = sActual.Length - 1
cbo.SelectionLength = sActual.Length - 1
cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
End If
End If
Loop Until bMatchFound
End Sub
End Module


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.