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

AutoComplete ComboBox in VB.Net

By , 7 May 2002
 

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:

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:

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

About the Author

Daryl
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: A little different code (vb.net version instead of c#)memberbwarehouse23 Feb '09 - 20:40 
Generalonly works when I type slowlymembermiko_tnt21 Nov '05 - 5:20 
QuestionWhat is DB.tblNameRow?membersanders.tec16 Sep '05 - 7:53 
AnswerRe: What is DB.tblNameRow?memberJohann Lazarus2 Feb '09 - 13:00 
Generalneeds improvementmembereletters_my30 Jan '05 - 19:48 
Generalautofill only works when I type slowsussRobert Gray20 Jun '04 - 14:59 
GeneralRe: autofill only works when I type slowmemberJoseph Hicks27 Jan '05 - 12:27 
GeneralRe: autofill only works when I type slowmemberTunca Bergmen3 Feb '05 - 3:07 
GeneralRe: autofill only works when I type slowsussAnonymous14 Apr '05 - 6:19 
GeneralSave Combo Boxmemberukjock1 Jan '04 - 7:34 
GeneralRe: Save Combo BoxmemberdELm5 Feb '04 - 15:22 
GeneralRe: Save Combo Boxsussaniepras30 Dec '04 - 7:24 
GeneralEscapesussAnonymous15 Oct '03 - 17:48 
GeneralDB.tblnameRowmembervb_me10 Sep '03 - 15:51 
GeneralJust One SubmemberLaurent Muller11 Aug '03 - 3:41 
GeneralRe: Just One SubmemberBehram9 Sep '03 - 13:29 
GeneralRe: Just One SubmemberLaurent Muller9 Sep '03 - 23:02 
GeneralRe: Just One SubsussAnonymous22 Nov '03 - 6:32 
GeneralRe: Just One Sub, slight change,memberkoo93 Dec '03 - 9:44 
GeneralRe: Just One Sub, slight change,sussMark Lam16 Feb '04 - 5:58 
GeneralRe: Just One Sub, slight change,memberPizza_spaghetti_giova_arezzoèé28 Apr '04 - 13:47 
GeneralRe: Just One Sub, slight change,sussAnonymous3 Mar '05 - 10:36 
GeneralRe: Just One Sub, slight change,memberclangl19 Jun '06 - 7:34 
GeneralRe: Just One SubsussAnonymous17 Jan '04 - 6:29 
GeneralRe: Just One Submemberaah_what1 Jul '04 - 20:48 
GeneralRe: Just One Submembermikah_9721 Sep '04 - 14:02 
GeneralRe: Just One SubsussAnonymous21 Sep '04 - 20:47 
GeneralRe: Just One SubmemberAlejandro K.28 Sep '04 - 7:50 
GeneralRe: Just One SubmemberM@@N29 Sep '04 - 3:35 
GeneralRe: Just One SubsussAnonymous19 Sep '05 - 13:18 
GeneralRe: Just One SubmemberDaniel8c26 Mar '06 - 17:05 
GeneralRe: Just One Subsussaniepras30 Dec '04 - 7:22 
GeneralRe: Just One SubsussAnonymous27 Mar '05 - 13:41 
GeneralRe: Just One SubmemberLaurent Muller27 Mar '05 - 23:09 
GeneralRe: Just One Submemberquick_dry3 Aug '05 - 23:02 
GeneralRe: Just One SubmemberiCeNET12 Aug '05 - 1:39 
GeneralRe: Just One SubmemberRevenueGuy18 May '06 - 8:55 
GeneralRe: Just One SubmemberTheFury9 Mar '08 - 13:14 
GeneralCVmembertuanhtc22 Jul '03 - 20:35 
GeneralRe: CVsussAnonymous16 Mar '04 - 10:22 
GeneralRe: CVsussAnonymous14 Feb '05 - 5:07 
GeneralRe: CVsussmarah16 Mar '04 - 10:23 
GeneralRe: CVmemberIamLamb7 Aug '04 - 19:25 
GeneralRe: CVmemberHeadRush3 Oct '04 - 14:18 
GeneralRe: CVmemberrt288013 Nov '04 - 15:38 
GeneralRe: CVsussAnonymous14 Feb '05 - 5:10 
GeneralAutoComplete ComboBox ClassmemberDDnDD4 Jun '03 - 13:25 
QuestionDB.tblNameRow??memberashrobo25 May '03 - 20:30 
AnswerRe: DB.tblNameRow??sussAnonymous20 Jul '04 - 11:52 

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

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