Click here to Skip to main content
13,400,143 members (42,657 online)
Click here to Skip to main content
Add your own
alternative version


114 bookmarked
Posted 21 Apr 2002

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

GeneralRe: Just One Sub, slight change, Pin
clangl19-Jun-06 8:34
memberclangl19-Jun-06 8:34 
GeneralRe: Just One Sub Pin
Anonymous17-Jan-04 7:29
sussAnonymous17-Jan-04 7:29 
GeneralRe: Just One Sub Pin
aah_what1-Jul-04 21:48
memberaah_what1-Jul-04 21:48 
GeneralRe: Just One Sub Pin
mikah_9721-Sep-04 15:02
membermikah_9721-Sep-04 15:02 
GeneralRe: Just One Sub Pin
Anonymous21-Sep-04 21:47
sussAnonymous21-Sep-04 21:47 
GeneralRe: Just One Sub Pin
Alejandro K.28-Sep-04 8:50
memberAlejandro K.28-Sep-04 8:50 
GeneralRe: Just One Sub Pin
M@@N29-Sep-04 4:35
memberM@@N29-Sep-04 4:35 
GeneralRe: Just One Sub Pin
Anonymous19-Sep-05 14:18
sussAnonymous19-Sep-05 14:18 
This is working for me except when I tab off of the control the text appears correct in the combobox but the cbo.text has a value of the text of the last selected item and that items selectedindex. The same things happens when if I use back space in the combobox and then tab off of the control.
GeneralRe: Just One Sub Pin
Daniel8c26-Mar-06 18:05
memberDaniel8c26-Mar-06 18:05 
GeneralRe: Just One Sub Pin
aniepras30-Dec-04 8:22
sussaniepras30-Dec-04 8:22 
GeneralRe: Just One Sub Pin
Anonymous27-Mar-05 14:41
sussAnonymous27-Mar-05 14:41 
GeneralRe: Just One Sub Pin
Laurent Muller28-Mar-05 0:09
memberLaurent Muller28-Mar-05 0:09 
GeneralRe: Just One Sub Pin
quick_dry4-Aug-05 0:02
memberquick_dry4-Aug-05 0:02 
GeneralRe: Just One Sub Pin
iCeNET12-Aug-05 2:39
memberiCeNET12-Aug-05 2:39 
GeneralRe: Just One Sub Pin
RevenueGuy18-May-06 9:55
memberRevenueGuy18-May-06 9:55 
GeneralRe: Just One Sub Pin
TheFury9-Mar-08 14:14
memberTheFury9-Mar-08 14:14 
GeneralCV Pin
tuanhtc22-Jul-03 21:35
membertuanhtc22-Jul-03 21:35 
GeneralRe: CV Pin
Anonymous16-Mar-04 11:22
sussAnonymous16-Mar-04 11:22 
GeneralRe: CV Pin
Anonymous14-Feb-05 6:07
sussAnonymous14-Feb-05 6:07 
GeneralRe: CV Pin
marah16-Mar-04 11:23
sussmarah16-Mar-04 11:23 
GeneralRe: CV Pin
IamLamb7-Aug-04 20:25
memberIamLamb7-Aug-04 20:25 
GeneralRe: CV Pin
HeadRush3-Oct-04 15:18
memberHeadRush3-Oct-04 15:18 
GeneralRe: CV Pin
rt288013-Nov-04 16:38
memberrt288013-Nov-04 16:38 
GeneralRe: CV Pin
Anonymous14-Feb-05 6:10
sussAnonymous14-Feb-05 6:10 
GeneralAutoComplete ComboBox Class Pin
DDnDD4-Jun-03 14:25
memberDDnDD4-Jun-03 14:25 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180218.2 | Last Updated 8 May 2002
Article Copyright 2002 by Daryl
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid