Click here to Skip to main content
12,302,493 members (65,100 online)
Click here to Skip to main content
Add your own
alternative version


113 bookmarked

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

You may also be interested in...

Comments and Discussions

QuestionThank you ! Pin
Member 96251486-Feb-14 12:03
memberMember 96251486-Feb-14 12:03 
GeneralRe: ComboBox Control in VB.Net Pin
Ankitaaguggi7-Oct-11 0:16
groupAnkitaaguggi7-Oct-11 0:16 
GeneralMy vote of 5 Pin
prodigytechnologies28-Apr-11 1:16
memberprodigytechnologies28-Apr-11 1:16 
GeneralThank you Pin
Zestelle16-Sep-09 5:02
memberZestelle16-Sep-09 5:02 
AnswerProblem in Access database query Pin
Ayesha.Hafeez16-Dec-08 5:35
memberAyesha.Hafeez16-Dec-08 5:35 
GeneralRe: Problem in Access database query Pin
bwarehouse23-Feb-09 16:01
memberbwarehouse23-Feb-09 16:01 
GeneralAutocomplete Question Pin
mchlcmprch29-Oct-07 13:29
membermchlcmprch29-Oct-07 13:29 
GeneralAutocomplete suggestion must try.. Pin
Samee Dars16-Mar-08 22:44
memberSamee Dars16-Mar-08 22:44 
GeneralRe: Autocomplete suggestion Pin
Samee Dars16-Mar-08 22:58
memberSamee Dars16-Mar-08 22:58 
GeneralRe: Autocomplete suggestion Pin
dibblm-Ohio9-Jan-09 5:31
memberdibblm-Ohio9-Jan-09 5:31 
Generalthank Pin
ansing20-Jan-07 3:31
memberansing20-Jan-07 3:31 
Generalthank Pin
ansing20-Jan-07 3:30
memberansing20-Jan-07 3:30 
QuestionDroppedDown Pin
andrewf2211-Jan-07 11:16
memberandrewf2211-Jan-07 11:16 
AnswerRe: DroppedDown Pin
paulmcl26-Jan-07 3:17
memberpaulmcl26-Jan-07 3:17 
I have the same problem and can't pin down where its going wrong. Until yesterday i thought my users were going mad reporting very intermittent problems, I wont say anything about my testing...

Seems to me that the updated value is definitely being saved but then overriden with the old value somewhere, eg as soon as we enter the leave event the old value is back. I've had a look through the events and properties but cant see anything you need to set to persist the value, eg like the way you handle the key events.

In the end I've had to resort to using a temporary form level object to store the autocomplete value of the combobox and a flag to indicate whether our autocomplete code has been used. in the leave event if flag == true point the combobox to the temporary object and reset the flag to false. I really dont like to do this as its a very bad solution so if anyone has a proper way i'd love to know.

GeneralRe: DroppedDown Pin
sephus615-Dec-08 11:24
membersephus615-Dec-08 11:24 
GeneralA little different code Pin
rtodosic31-Mar-06 7:15
memberrtodosic31-Mar-06 7:15 
GeneralRe: A little different code ( version instead of c#) Pin
bwarehouse23-Feb-09 20:40
memberbwarehouse23-Feb-09 20:40 
Generalonly works when I type slowly Pin
miko_tnt21-Nov-05 5:20
membermiko_tnt21-Nov-05 5:20 
QuestionWhat is DB.tblNameRow? Pin
sanders.tec16-Sep-05 7:53
membersanders.tec16-Sep-05 7:53 
AnswerRe: What is DB.tblNameRow? Pin
Johann Lazarus2-Feb-09 13:00
memberJohann Lazarus2-Feb-09 13:00 
Generalneeds improvement Pin
eletters_my30-Jan-05 19:48
membereletters_my30-Jan-05 19:48 
Generalautofill only works when I type slow Pin
Robert Gray20-Jun-04 14:59
sussRobert Gray20-Jun-04 14:59 
GeneralRe: autofill only works when I type slow Pin
Joseph Hicks27-Jan-05 12:27
memberJoseph Hicks27-Jan-05 12:27 
GeneralRe: autofill only works when I type slow Pin
Tunca Bergmen3-Feb-05 3:07
memberTunca Bergmen3-Feb-05 3:07 
GeneralRe: autofill only works when I type slow Pin
Anonymous14-Apr-05 6:19
sussAnonymous14-Apr-05 6:19 
GeneralSave Combo Box Pin
ukjock1-Jan-04 7:34
memberukjock1-Jan-04 7:34 
GeneralRe: Save Combo Box Pin
dELm5-Feb-04 15:22
memberdELm5-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
sussAnonymous15-Oct-03 17:48 
GeneralDB.tblnameRow Pin
vb_me10-Sep-03 15:51
membervb_me10-Sep-03 15:51 
GeneralJust One Sub Pin
Laurent Muller11-Aug-03 3:41
memberLaurent Muller11-Aug-03 3:41 
GeneralRe: Just One Sub Pin
Behram9-Sep-03 13:29
memberBehram9-Sep-03 13:29 
GeneralRe: Just One Sub Pin
Laurent Muller9-Sep-03 23:02
memberLaurent Muller9-Sep-03 23:02 
GeneralRe: Just One Sub Pin
Anonymous22-Nov-03 6:32
sussAnonymous22-Nov-03 6:32 
GeneralRe: Just One Sub, slight change, Pin
koo93-Dec-03 9:44
memberkoo93-Dec-03 9:44 
GeneralRe: Just One Sub, slight change, Pin
Mark Lam16-Feb-04 5:58
sussMark Lam16-Feb-04 5:58 
GeneralRe: Just One Sub, slight change, Pin
Pizza_spaghetti_giova_arezzoèé28-Apr-04 13:47
memberPizza_spaghetti_giova_arezzoèé28-Apr-04 13:47 
GeneralRe: Just One Sub, slight change, Pin
Anonymous3-Mar-05 10:36
sussAnonymous3-Mar-05 10:36 
GeneralRe: Just One Sub, slight change, Pin
clangl19-Jun-06 7:34
memberclangl19-Jun-06 7:34 
GeneralRe: Just One Sub Pin
Anonymous17-Jan-04 6:29
sussAnonymous17-Jan-04 6:29 
GeneralRe: Just One Sub Pin
aah_what1-Jul-04 20:48
memberaah_what1-Jul-04 20:48 
GeneralRe: Just One Sub Pin
mikah_9721-Sep-04 14:02
membermikah_9721-Sep-04 14:02 
GeneralRe: Just One Sub Pin
Anonymous21-Sep-04 20:47
sussAnonymous21-Sep-04 20:47 
GeneralRe: Just One Sub Pin
Alejandro K.28-Sep-04 7:50
memberAlejandro K.28-Sep-04 7:50 
GeneralRe: Just One Sub Pin
M@@N29-Sep-04 3:35
memberM@@N29-Sep-04 3:35 
GeneralRe: Just One Sub Pin
Anonymous19-Sep-05 13:18
sussAnonymous19-Sep-05 13:18 
GeneralRe: Just One Sub Pin
Daniel8c26-Mar-06 17:05
memberDaniel8c26-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
sussAnonymous27-Mar-05 13:41 
GeneralRe: Just One Sub Pin
Laurent Muller27-Mar-05 23:09
memberLaurent Muller27-Mar-05 23:09 

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.

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