Click here to Skip to main content
15,860,943 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 521.3K   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

 
GeneralMy vote of 1 Pin
MDT MTO4-Aug-22 2:37
MDT MTO4-Aug-22 2:37 
QuestionThank you ! Pin
Member 96251486-Feb-14 12:03
Member 96251486-Feb-14 12:03 
GeneralRe: ComboBox Control in VB.Net Pin
Ankitaaguggi7-Oct-11 0:16
Ankitaaguggi7-Oct-11 0:16 
GeneralMy vote of 5 Pin
prodigytechnologies28-Apr-11 1:16
prodigytechnologies28-Apr-11 1:16 
GeneralThank you Pin
Zestelle16-Sep-09 5:02
Zestelle16-Sep-09 5:02 
AnswerProblem in Access database query Pin
Ayesha.Hafeez16-Dec-08 5:35
Ayesha.Hafeez16-Dec-08 5:35 
GeneralRe: Problem in Access database query Pin
bwarehouse23-Feb-09 16:01
bwarehouse23-Feb-09 16:01 
GeneralAutocomplete Question Pin
mchlcmprch29-Oct-07 13:29
mchlcmprch29-Oct-07 13:29 
GeneralAutocomplete suggestion must try.. Pin
Samee Daris16-Mar-08 22:44
Samee Daris16-Mar-08 22:44 
GeneralRe: Autocomplete suggestion Pin
Samee Daris16-Mar-08 22:58
Samee Daris16-Mar-08 22:58 
GeneralRe: Autocomplete suggestion Pin
dibblm-Ohio9-Jan-09 5:31
dibblm-Ohio9-Jan-09 5:31 
Its works for me. thank you soooooo much.

Im a fairly novice programmer and I dont know how to state what Im looking for. I ran into the original post. Saw all the code he had to go through and decided to see if someone had modified it any when I ran into your post.

Works like a charm. Only the page flutters a couple time after you select a record. Any idea why ?

Mike Dibble

Generalthank Pin
ansing20-Jan-07 3:31
ansing20-Jan-07 3:31 
Generalthank Pin
ansing20-Jan-07 3:30
ansing20-Jan-07 3:30 
QuestionDroppedDown Pin
andrewf2211-Jan-07 11:16
andrewf2211-Jan-07 11:16 
AnswerRe: DroppedDown Pin
paulmcl26-Jan-07 3:17
paulmcl26-Jan-07 3:17 
GeneralRe: DroppedDown Pin
sephus615-Dec-08 11:24
sephus615-Dec-08 11:24 
GeneralA little different code Pin
rtodosic31-Mar-06 7:15
rtodosic31-Mar-06 7:15 
GeneralRe: A little different code (vb.net version instead of c#) Pin
bwarehouse23-Feb-09 20:40
bwarehouse23-Feb-09 20:40 
Generalonly works when I type slowly Pin
miko_tnt21-Nov-05 5:20
miko_tnt21-Nov-05 5:20 
QuestionWhat is DB.tblNameRow? Pin
sanders-tec16-Sep-05 7:53
sanders-tec16-Sep-05 7:53 
AnswerRe: What is DB.tblNameRow? Pin
Johann Lazarus2-Feb-09 13:00
Johann Lazarus2-Feb-09 13:00 
Generalneeds improvement Pin
eletters_my30-Jan-05 19:48
eletters_my30-Jan-05 19:48 
Generalautofill only works when I type slow Pin
Robert Gray20-Jun-04 14:59
Robert Gray20-Jun-04 14:59 
GeneralRe: autofill only works when I type slow Pin
Joseph Hicks27-Jan-05 12:27
Joseph Hicks27-Jan-05 12:27 
GeneralRe: autofill only works when I type slow Pin
Tunca Bergmen3-Feb-05 3:07
Tunca Bergmen3-Feb-05 3:07 

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.