Click here to Skip to main content
15,867,297 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.5K   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: Just One Sub Pin
Laurent Muller27-Mar-05 23:09
professionalLaurent Muller27-Mar-05 23:09 
GeneralRe: Just One Sub Pin
quick_dry3-Aug-05 23:02
quick_dry3-Aug-05 23:02 
GeneralRe: Just One Sub Pin
iCeNET12-Aug-05 1:39
iCeNET12-Aug-05 1:39 
GeneralRe: Just One Sub Pin
RevenueGuy18-May-06 8:55
RevenueGuy18-May-06 8:55 
GeneralRe: Just One Sub Pin
TheFury9-Mar-08 13:14
TheFury9-Mar-08 13:14 
GeneralCV Pin
tuanhtc22-Jul-03 20:35
tuanhtc22-Jul-03 20:35 
GeneralRe: CV Pin
Anonymous16-Mar-04 10:22
Anonymous16-Mar-04 10:22 
GeneralRe: CV Pin
Anonymous14-Feb-05 5:07
Anonymous14-Feb-05 5:07 
GeneralRe: CV Pin
Marah16-Mar-04 10:23
Marah16-Mar-04 10:23 
GeneralRe: CV Pin
IamLamb7-Aug-04 19:25
IamLamb7-Aug-04 19:25 
GeneralRe: CV Pin
HeadRush3-Oct-04 14:18
HeadRush3-Oct-04 14:18 
GeneralRe: CV Pin
Member 151321613-Nov-04 15:38
Member 151321613-Nov-04 15:38 
GeneralRe: CV Pin
Anonymous14-Feb-05 5:10
Anonymous14-Feb-05 5:10 
GeneralAutoComplete ComboBox Class Pin
DDnDD4-Jun-03 13:25
DDnDD4-Jun-03 13:25 
QuestionDB.tblNameRow?? Pin
ashrobo25-May-03 20:30
ashrobo25-May-03 20:30 
AnswerRe: DB.tblNameRow?? Pin
Anonymous20-Jul-04 11:52
Anonymous20-Jul-04 11:52 

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.