Click here to Skip to main content
Click here to Skip to main content

AutoComplete ComboBox in VB.Net

, 7 May 2002
Rate this:
Please Sign up or sign in to vote.
2 Simple Functions to call from ComboBox events to Autocomplete. Will work with Data Bound combos.
<!-- Add the rest of your HTML here -->


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:

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


    '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:

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
    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


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


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

QuestionThank you ! PinmemberMember 96251486-Feb-14 13:03 
GeneralRe: ComboBox Control in VB.Net PingroupAnkitaaguggi7-Oct-11 1:16 
GeneralMy vote of 5 Pinmemberprodigytechnologies28-Apr-11 2:16 
GeneralThank you PinmemberZestelle16-Sep-09 6:02 
AnswerProblem in Access database query PinmemberAyesha.Hafeez16-Dec-08 6:35 
GeneralRe: Problem in Access database query Pinmemberbwarehouse23-Feb-09 17:01 
GeneralAutocomplete Question Pinmembermchlcmprch29-Oct-07 14:29 
I seem to be (as usual) confused by the behaviour of autocomplete combos. Is it my imagination or do autocomplete combos change their selected indexes when they are selected? I have an app where this seems to be happening... a little background...
On a winform I have three combos all populated from the same arraylist.A concept number, an item number and a name, all related and associated. there are occasions when, for instance, I have set the selectedindex to, say 122, via selecting a concept number, and when I click or tab to the next combo, the selected indexes seem to be set to 0! Very irritating. This is definately in some way due to autocomplete, because if I make them all autocomplete false this doesn't happen. Unfortunately there is a reqirement for autocomplete.... I'm really frustrated and all my googling has not yet gotten me an solution!
Michael S. Comperchio
203 218 4053
GeneralAutocomplete suggestion must try.. PinmemberSamee Dars16-Mar-08 23:44 
GeneralRe: Autocomplete suggestion PinmemberSamee Dars16-Mar-08 23:58 
GeneralRe: Autocomplete suggestion Pinmemberdibblm-Ohio9-Jan-09 6:31 
Generalthank Pinmemberansing20-Jan-07 4:31 
Generalthank Pinmemberansing20-Jan-07 4:30 
QuestionDroppedDown Pinmemberandrewf2211-Jan-07 12:16 
AnswerRe: DroppedDown Pinmemberpaulmcl26-Jan-07 4:17 
GeneralRe: DroppedDown Pinmembersephus615-Dec-08 12:24 
GeneralA little different code Pinmemberrtodosic31-Mar-06 8:15 
GeneralRe: A little different code ( version instead of c#) Pinmemberbwarehouse23-Feb-09 21:40 
Generalonly works when I type slowly Pinmembermiko_tnt21-Nov-05 6:20 
QuestionWhat is DB.tblNameRow? Pinmembersanders.tec16-Sep-05 8:53 
AnswerRe: What is DB.tblNameRow? PinmemberJohann Lazarus2-Feb-09 14:00 
Generalneeds improvement Pinmembereletters_my30-Jan-05 20:48 
Generalautofill only works when I type slow PinsussRobert Gray20-Jun-04 15:59 
GeneralRe: autofill only works when I type slow PinmemberJoseph Hicks27-Jan-05 13:27 
GeneralRe: autofill only works when I type slow PinmemberTunca Bergmen3-Feb-05 4:07 
GeneralRe: autofill only works when I type slow PinsussAnonymous14-Apr-05 7:19 
GeneralSave Combo Box Pinmemberukjock1-Jan-04 8:34 
GeneralRe: Save Combo Box PinmemberdELm5-Feb-04 16:22 
GeneralRe: Save Combo Box Pinsussaniepras30-Dec-04 8:24 
GeneralEscape PinsussAnonymous15-Oct-03 18:48 
GeneralDB.tblnameRow Pinmembervb_me10-Sep-03 16:51 
GeneralJust One Sub PinmemberLaurent Muller11-Aug-03 4:41 
GeneralRe: Just One Sub PinmemberBehram9-Sep-03 14:29 
GeneralRe: Just One Sub PinmemberLaurent Muller10-Sep-03 0:02 
GeneralRe: Just One Sub PinsussAnonymous22-Nov-03 7:32 
GeneralRe: Just One Sub, slight change, Pinmemberkoo93-Dec-03 10:44 
GeneralRe: Just One Sub, slight change, PinsussMark Lam16-Feb-04 6:58 
GeneralRe: Just One Sub, slight change, PinmemberPizza_spaghetti_giova_arezzoèé28-Apr-04 14:47 
GeneralRe: Just One Sub, slight change, PinsussAnonymous3-Mar-05 11:36 
GeneralRe: Just One Sub, slight change, Pinmemberclangl19-Jun-06 8:34 
GeneralRe: Just One Sub PinsussAnonymous17-Jan-04 7:29 
GeneralRe: Just One Sub Pinmemberaah_what1-Jul-04 21:48 
GeneralRe: Just One Sub Pinmembermikah_9721-Sep-04 15:02 
GeneralRe: Just One Sub PinsussAnonymous21-Sep-04 21:47 
GeneralRe: Just One Sub PinmemberAlejandro K.28-Sep-04 8:50 
GeneralRe: Just One Sub PinmemberM@@N29-Sep-04 4:35 
GeneralRe: Just One Sub PinsussAnonymous19-Sep-05 14:18 
GeneralRe: Just One Sub PinmemberDaniel8c26-Mar-06 18:05 
GeneralRe: Just One Sub Pinsussaniepras30-Dec-04 8:22 
GeneralRe: Just One Sub PinsussAnonymous27-Mar-05 14:41 
GeneralRe: Just One Sub PinmemberLaurent Muller28-Mar-05 0:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 8 May 2002
Article Copyright 2002 by Daryl
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid