Click here to Skip to main content
15,880,427 members
Articles / Programming Languages / Visual Basic
Article

AutoComplete ComboBox in VB.Net

Rate me:
Please Sign up or sign in to vote.
4.63/5 (52 votes)
7 May 2002 522.1K   115   66
2 Simple Functions to call from ComboBox events to Autocomplete. Will work with Data Bound combos.

Introduction

This is an AutoCompleting ComboBox that works with Data Bound or Regular ComboBoxes in VB.NET. As you type the case is preserved but the remaining text is auto filled by the list items. When the Leave function is called the case is fixed and the index from the list is also selected if there is a matching item.

Sample Image - AutoComplete_ComboBox.gif

Call the corresponding functions from your Combobox's KeyUp and Leave events like so:

VB
Private Sub cboName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) 
                                                            Handles cboName.Leave
    Dim recRowView As DataRowView
    Dim recName As DB.tblNameRow

    AutoCompleteCombo_Leave(cboName)

    'OPTIONAL: Now you can  do some extra handling if you want

    'Get the Selected Record from my Data Bound Combo (Return Type is DataRowView)
    recRowView = cboName.SelectedItem
    If recRowView Is Nothing Then Exit Sub

    'Display the Name Info (Row Type comes from my bound Dataset)
    recName = recRowView.Row
    lblAccountNum.Text = recName.AccountNum
    lblCompanyName.Text = recName.CompanyName

End Sub

Private Sub cboName_KeyUp(ByVal sender As Object, 
              ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboName.KeyUp

    AutoCompleteCombo_KeyUp(cboName, e)

End Sub

Here are the Generic Functions for handling the events:

VB
Public Sub AutoCompleteCombo_KeyUp(ByVal cbo As ComboBox, ByVal e As KeyEventArgs)
    Dim sTypedText As String
    Dim iFoundIndex As Integer
    Dim oFoundItem As Object
    Dim sFoundText As String
    Dim sAppendText As String

    'Allow select keys without Autocompleting
    Select Case e.KeyCode
        Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down
            Return
    End Select

    'Get the Typed Text and Find it in the list
    sTypedText = cbo.Text
    iFoundIndex = cbo.FindString(sTypedText)

    'If we found the Typed Text in the list then Autocomplete
    If iFoundIndex >= 0 Then

        'Get the Item from the list (Return Type depends if Datasource was bound 
        ' or List Created)
        oFoundItem = cbo.Items(iFoundIndex)

        'Use the ListControl.GetItemText to resolve the Name in case the Combo 
        ' was Data bound
        sFoundText = cbo.GetItemText(oFoundItem)

        'Append then found text to the typed text to preserve case
        sAppendText = sFoundText.Substring(sTypedText.Length)
        cbo.Text = sTypedText & sAppendText

        'Select the Appended Text
        cbo.SelectionStart = sTypedText.Length
        cbo.SelectionLength = sAppendText.Length

    End If

End Sub


Public Sub AutoCompleteCombo_Leave(ByVal cbo As ComboBox)
    Dim iFoundIndex As Integer

    iFoundIndex = cbo.FindStringExact(cbo.Text)

    cbo.SelectedIndex = iFoundIndex

End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: autofill only works when I type slow Pin
Anonymous14-Apr-05 6:19
Anonymous14-Apr-05 6:19 
GeneralSave Combo Box Pin
ukjock1-Jan-04 7:34
ukjock1-Jan-04 7:34 
GeneralRe: Save Combo Box Pin
dELm5-Feb-04 15:22
dELm5-Feb-04 15:22 
GeneralRe: Save Combo Box Pin
aniepras30-Dec-04 7:24
sussaniepras30-Dec-04 7:24 
GeneralEscape Pin
Anonymous15-Oct-03 17:48
Anonymous15-Oct-03 17:48 
GeneralDB.tblnameRow Pin
vb_me10-Sep-03 15:51
vb_me10-Sep-03 15:51 
GeneralJust One Sub Pin
Laurent Muller11-Aug-03 3:41
professionalLaurent Muller11-Aug-03 3:41 
GeneralRe: Just One Sub Pin
Behram9-Sep-03 13:29
Behram9-Sep-03 13:29 
GeneralRe: Just One Sub Pin
Laurent Muller9-Sep-03 23:02
professionalLaurent Muller9-Sep-03 23:02 
GeneralRe: Just One Sub Pin
Anonymous22-Nov-03 6:32
Anonymous22-Nov-03 6:32 
GeneralRe: Just One Sub, slight change, Pin
koo93-Dec-03 9:44
koo93-Dec-03 9:44 
GeneralRe: Just One Sub, slight change, Pin
Mark Lam16-Feb-04 5:58
Mark Lam16-Feb-04 5:58 
GeneralRe: Just One Sub, slight change, Pin
Pizza_spaghetti_giova_arezzoèé28-Apr-04 13:47
Pizza_spaghetti_giova_arezzoèé28-Apr-04 13:47 
GeneralRe: Just One Sub, slight change, Pin
Anonymous3-Mar-05 10:36
Anonymous3-Mar-05 10:36 
GeneralRe: Just One Sub, slight change, Pin
clangl19-Jun-06 7:34
clangl19-Jun-06 7:34 
GeneralRe: Just One Sub Pin
Anonymous17-Jan-04 6:29
Anonymous17-Jan-04 6:29 
GeneralRe: Just One Sub Pin
aah_what1-Jul-04 20:48
aah_what1-Jul-04 20:48 
GeneralRe: Just One Sub Pin
mikah_9721-Sep-04 14:02
mikah_9721-Sep-04 14:02 
GeneralRe: Just One Sub Pin
Anonymous21-Sep-04 20:47
Anonymous21-Sep-04 20:47 
GeneralRe: Just One Sub Pin
Alejandro K.28-Sep-04 7:50
Alejandro K.28-Sep-04 7:50 
GeneralRe: Just One Sub Pin
M@@N29-Sep-04 3:35
M@@N29-Sep-04 3:35 
GeneralRe: Just One Sub Pin
Anonymous19-Sep-05 13:18
Anonymous19-Sep-05 13:18 
GeneralRe: Just One Sub Pin
Daniel8c26-Mar-06 17:05
Daniel8c26-Mar-06 17:05 
GeneralRe: Just One Sub Pin
aniepras30-Dec-04 7:22
sussaniepras30-Dec-04 7:22 
GeneralRe: Just One Sub Pin
Anonymous27-Mar-05 13:41
Anonymous27-Mar-05 13:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.