Click here to Skip to main content
Licence 
First Posted 21 Apr 2002
Views 357,870
Bookmarked 109 times

AutoComplete ComboBox in VB.Net

By | 7 May 2002 | Article
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:

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



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: ComboBox Control in VB.Net PingroupAnkitaaguggi0:16 7 Oct '11  
GeneralMy vote of 5 Pinmemberprodigytechnologies1:16 28 Apr '11  
GeneralThank you PinmemberZestelle5:02 16 Sep '09  
AnswerProblem in Access database query PinmemberAyesha.Hafeez5:35 16 Dec '08  
GeneralRe: Problem in Access database query Pinmemberbwarehouse16:01 23 Feb '09  
GeneralAutocomplete Question Pinmembermchlcmprch13:29 29 Oct '07  
GeneralAutocomplete suggestion must try.. PinmemberSamee Dars22:44 16 Mar '08  
GeneralRe: Autocomplete suggestion PinmemberSamee Dars22:58 16 Mar '08  
GeneralRe: Autocomplete suggestion Pinmemberdibblm-Ohio5:31 9 Jan '09  
Generalthank Pinmemberansing3:31 20 Jan '07  
Generalthank Pinmemberansing3:30 20 Jan '07  
QuestionDroppedDown Pinmemberandrewf2211:16 11 Jan '07  
AnswerRe: DroppedDown Pinmemberpaulmcl3:17 26 Jan '07  
GeneralRe: DroppedDown Pinmembersephus611:24 15 Dec '08  
GeneralA little different code Pinmemberrtodosic7:15 31 Mar '06  
GeneralRe: A little different code (vb.net version instead of c#) Pinmemberbwarehouse20:40 23 Feb '09  
Generalonly works when I type slowly Pinmembermiko_tnt5:20 21 Nov '05  
QuestionWhat is DB.tblNameRow? Pinmembersanders.tec7:53 16 Sep '05  
AnswerRe: What is DB.tblNameRow? PinmemberJohann Lazarus13:00 2 Feb '09  
Generalneeds improvement Pinmembereletters_my19:48 30 Jan '05  
Generalautofill only works when I type slow PinsussRobert Gray14:59 20 Jun '04  
GeneralRe: autofill only works when I type slow PinmemberJoseph Hicks12:27 27 Jan '05  
I've had this problem in the past as well.   I found that the problem would be when a 2nd (or 3rd, 4th, etc.) key was pressed before the first key was released.   This wouldn't be an issue for someone with only one finger, but for the rest of us my solution is as follows.
 
Declaration of my control:
 
Public Class AutoFillComboBox
      Inherits ComboBox
 
First add a private data member to store the most recent keydown key:
 
Private _LastKeyDown As System.Windows.Forms.KeyEventArgs
 
In the KeyDown event of my object, I'll store the key that was pressed:
 
Protected Overrides Sub OnKeyDown(ByVal e as System.Windows.Forms.KeyEventArgs)
      MyBase.OnKeyDown(e)
      _LastKeyDown = e
End Sub
 
And finally, the first thing I do in the KeyUp event is to ensure that the key that was released was actually the last key pressed (ie: ignore the KeyUp unless it was also the last KeyDown).   Also, a key may have been held down before the control was loaded, so if the last KeyDown doesn't exist (Is Nothing), ignore that too:
 
Protected Overrides Sub OnKeyUp(ByVal e as System.Windows.Forms.KeyEventArgs)
      MyBase.OnKeyUp(e)
      If (_LastKeyDown Is Nothing) = True OrElse (e.KeyCode = _LastKeyDown.KeyCode) = False Then Exit Sub
      ....... processing to AutoComplete .......
End Sub
 
I'm sure that using overrides is unnecessary, and you could probably just store the e.KeyCode value in a System.Windows.Forms.Keys variable, but that's not the point. Wink | ;)    Hope this helps someone out there in the future...
 
Joseph Hicks
jhicks<AT>ifmc.org
GeneralRe: autofill only works when I type slow PinmemberTunca Bergmen3:07 3 Feb '05  
GeneralRe: autofill only works when I type slow PinsussAnonymous6:19 14 Apr '05  
GeneralSave Combo Box Pinmemberukjock7:34 1 Jan '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120528.1 | Last Updated 8 May 2002
Article Copyright 2002 by Daryl
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid