March 5, 2021
Hot Topics:

Windows Secrets for Visual Basic, Part 1

  • By Karl Moore
  • Send Email »
  • More Articles »

Save User Time: Add AutoComplete to Combo Boxes

Develop an application in a program such as Microsoft Access and all your combo boxes will incorporate "AutoComplete" by default, that ability to be able to tap a few characters in a drop down and have the nearest matching selection picked out for you.

In Visual Basic however, there's no such intrinsic support. If you want AutoComplete, you've got to do it yourself. And this tip shows you how.

Simply add the following methods to your form. The first is called AutoCompleteKeyUp and accepts a combo box and KeyEventArgs object as arguments. You need to call these in the KeyUp event of your combo box: it looks at what the user has typed and selects the most appropriate match. The second is called AutoCompleteLeave and should be called when the Leave event of your combo box is fired. This one simply takes whatever you've finally chosen and cases it properly, as per the matching selection in the combo box.

Let's look at those functions now:

Public Sub AutoCompleteKeyUp(ByVal Combo 

As ComboBox, _
    ByVal e As KeyEventArgs)
    Dim strTyped As String
    Dim intFoundIndex As Integer
    Dim objFoundItem As Object
    Dim strFoundText As String
    Dim strAppendText As String
    ' Ignore basic selection keys
    Select Case e.KeyCode
        Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, _
            Keys.Delete, Keys.Down, Keys.CapsLock
    End Select
    ' Find what user has typed in list
    strTyped = Combo.Text
    intFoundIndex = Combo.FindString(strTyped)
    ' If found...
    If intFoundIndex >= 0 Then
        ' Get list item (actual type depends on whether data 

        objFoundItem = Combo.Items(intFoundIndex)
        ' Use control to resolve text - in case data bound
        strFoundText = Combo.GetItemText(objFoundItem)
        ' Append the typed text to rest of the found string
        ' (text is set twice due to a combo box quirk:
        '  on certain platforms, setting just once ignores casing!)
        strAppendText = strFoundText.Substring(strTyped.Length)
        Combo.Text = strTyped & strAppendText
        Combo.Text = strTyped & strAppendText
        ' Select the appended text
        Combo.SelectionStart = strTyped.Length
        Combo.SelectionLength = strAppendText.Length
    End If
End Sub

Public Sub AutoCompleteLeave(ByVal Combo As ComboBox)
    ' Correct casing when leaving combo
    Dim intFoundIndex As Integer
    intFoundIndex = Combo.FindStringExact(Combo.Text)
    Combo.SelectedIndex = -1
    Combo.SelectedIndex = intFoundIndex
End Sub

And here's how you may call these functions from your combo box:

    Private Sub ComboBox1_KeyUp(ByVal 

sender As Object, _
        ByVal e As 

System.Windows.Forms.KeyEventArgs) _
           Handles ComboBox1.KeyUp
        AutoCompleteKeyUp(ComboBox1, e) 
    End Sub
    Private Sub ComboBox1_Leave(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.Leave
    End Sub

That's all you need to create your own AutoComplete combo boxes. And if you're feeling really adventurous, you might even want to wrap all of this up into a neat little user control. But we'll leave that for another tip...

Figure 2: Our AutoComplete combo box strutting its stuff.

Page 4 of 5

This article was originally published on January 21, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date