Click here to Skip to main content
6,822,613 members and growing! (20,367 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

AutoComplete ComboBox in VB.Net

By Daryl

2 Simple Functions to call from ComboBox events to Autocomplete. Will work with Data Bound combos.
VB, Windows, .NET1.0, Visual-Studio, Dev
Posted:21 Apr 2002
Updated:7 May 2002
Views:296,196
Bookmarked:104 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
54 votes for this article.
Popularity: 7.54 Rating: 4.35 out of 5
1 vote, 2.3%
1

2
3 votes, 7.0%
3
11 votes, 25.6%
4
28 votes, 65.1%
5

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


Member

Occupation: Web Developer
Location: United States United States

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 62 (Total in Forum: 62) (Refresh)FirstPrevNext
GeneralThank you PinmemberZestelle6:02 16 Sep '09  
AnswerProblem in Access database query PinmemberAyesha.Hafeez6:35 16 Dec '08  
GeneralRe: Problem in Access database query Pinmemberbwarehouse17:01 23 Feb '09  
GeneralAutocomplete Question Pinmembermchlcmprch14:29 29 Oct '07  
GeneralAutocomplete suggestion must try.. PinmemberSamee Dars23:44 16 Mar '08  
GeneralRe: Autocomplete suggestion PinmemberSamee Dars23:58 16 Mar '08  
GeneralRe: Autocomplete suggestion Pinmemberdibblm-Ohio6:31 9 Jan '09  
Generalthank Pinmemberansing4:31 20 Jan '07  
Generalthank Pinmemberansing4:30 20 Jan '07  
QuestionDroppedDown Pinmemberandrewf2212:16 11 Jan '07  
AnswerRe: DroppedDown Pinmemberpaulmcl4:17 26 Jan '07  
GeneralRe: DroppedDown Pinmembersephus612:24 15 Dec '08  
GeneralA little different code Pinmemberrtodosic8:15 31 Mar '06  
GeneralRe: A little different code (vb.net version instead of c#) Pinmemberbwarehouse21:40 23 Feb '09  
Generalonly works when I type slowly Pinmembermiko_tnt6:20 21 Nov '05  
GeneralWhat is DB.tblNameRow? Pinmembersanders.tec8:53 16 Sep '05  
GeneralRe: What is DB.tblNameRow? PinmemberJohann Lazarus14:00 2 Feb '09  
Generalneeds improvement Pinmembereletters_my20:48 30 Jan '05  
Generalautofill only works when I type slow PinsussRobert Gray15:59 20 Jun '04  
GeneralRe: autofill only works when I type slow PinmemberJoseph Hicks13:27 27 Jan '05  
GeneralRe: autofill only works when I type slow PinmemberTunca Bergmen4:07 3 Feb '05  
GeneralRe: autofill only works when I type slow PinsussAnonymous7:19 14 Apr '05  
GeneralSave Combo Box Pinmemberukjock8:34 1 Jan '04  
GeneralRe: Save Combo Box PinmemberdELm16:22 5 Feb '04  
GeneralRe: Save Combo Box Pinsussaniepras8:24 30 Dec '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 7 May 2002
Editor: Chris Maunder
Copyright 2002 by Daryl
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project