We help IT Professionals succeed at work.

Combobox Autofill in VB .NET

jellybungsters
jellybungsters asked
on
Does anyone have any sample code for performing an autofill feature for comboboxes in VB .NET?

In other words, the combobox checks each keystroke the user presses and sees if the text entered matches any entries in the combobox. If a match is found, the combobox is filled with the entry. However, the cursor needs to remain after the last key entered.

For example, if the user enters "A", the combobox displays the first entry found beginning with "A". Then if the user enters "B", it moves to the first entry beginning with "AB". When they press backspace, it moves back to the first entry beginning with "A".
Comment
Watch Question

forgive me if i'm wrong but i thought that vb.net cbo box had this feature built in ??
If it is built in I couldn't find a way to enable it...

Here is some simple code I use to achieve autocomplete in a ComboBox...

(VB.NET)
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

    Dim index As Integer
    Dim FindString As String

    If Asc(e.KeyChar) = Keys.Escape Then
        ComboBox1.SelectedIndex = -1
        ComboBox1.Text = ""
    Else
        FindString = ComboBox1.Text
        index = ComboBox1.FindString(FindString)

        If index <> -1 Then
            ComboBox1.SelectedIndex = index
            ComboBox1.SelectionStart = FindString.Length
            ComboBox1.SelectionLength = ComboBox1.Text.Length - ComboBox1.SelectionStart
        End If

    End If

    e.Handled = True

End Sub
The code above works, but there is a problem if you use the BACKSPACE key... here is the fixed version...

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
    Dim FindString As String
    If Asc(e.KeyChar) = Keys.Escape Then
        ComboBox1.SelectedIndex = -1
        ComboBox1.Text = ""
    ElseIf Asc(e.KeyChar) = Keys.Back Then
        If ComboBox1.Text.Length > 0 Then
            ComboBoxAutoComplete(ComboBox1, ComboBox1.Text.Remove(ComboBox1.Text.Length - 1, 1))
        End If
    Else
        ComboBoxAutoComplete(ComboBox1, ComboBox1.Text)
    End If
    e.Handled = True
End Sub

Private Sub ComboBoxAutoComplete(ByVal combo As ComboBox, ByVal str As String)
    Dim index As Integer
    If str.Length = 0 Then
        combo.SelectedIndex = -1
        combo.Text = ""
    Else
        index = combo.FindString(str)
        If index <> -1 Then
            combo.SelectedIndex = index
            combo.SelectionStart = str.Length
            combo.SelectionLength = combo.Text.Length - combo.SelectionStart
        End If
    End If
End Sub

Author

Commented:
There is a slight problem if you enter 2 characters and the first character does not match any item in the list. Pressing backspace in this situation erases both characters, not just the 2nd one. I was able to tweek the code a little bit to solve this problem.
I have tweaked the code above to make it work without errors. Here it is...
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
    Dim FindString As String
    If Asc(e.KeyChar) = Keys.Escape Then
      ComboBox1.SelectedIndex = -1
      ComboBox1.Text = ""
    ElseIf Asc(e.KeyChar) = Keys.Back Then
      If ComboBox1.Text.Length > 0 Then
        FindString = Replace(ComboBox1.Text, (ComboBox1.SelectedText), "")
        If Not FindString = "" Then FindString = Microsoft.VisualBasic.Left(FindString, Len(FindString) - 1)
        ComboBoxAutoComplete(ComboBox1, FindString)
      End If
    ElseIf Asc(e.KeyChar) = Keys.Up Or Asc(e.KeyChar) = Keys.Down Then
      ComboBoxAutoComplete(ComboBox1, ComboBox1.Text)
    Else
      FindString = Replace(ComboBox1.Text, (ComboBox1.SelectedText), "") & e.KeyChar
      ComboBoxAutoComplete(ComboBox1, FindString)
    End If
    e.Handled = True
  End Sub
  Private Sub ComboBoxAutoComplete(ByVal combo As ComboBox, ByVal str As String)
    Dim index As Integer
    If str.Length = 0 Then
      combo.SelectedIndex = -1
      combo.Text = ""
    Else
      index = combo.FindString(str)
      If index <> -1 Then
        combo.SelectedIndex = index
        combo.SelectionStart = str.Length
        combo.SelectionLength = combo.Text.Length - combo.SelectionStart
      End If
    End If
  End Sub
  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    If Not ComboBox1.SelectedIndex = -1 Then
      ' Put code here to do work when new item in combobox is selected
    End If
  End Sub

Open in new window

Forgot to remove the following code snippet since it isn't handled by the System.Windows.Forms.KeyPressEventArgs

ElseIf Asc(e.KeyChar) = Keys.Up Or Asc(e.KeyChar) = Keys.Down Then
      ComboBoxAutoComplete(ComboBox1, ComboBox1.Text)
Pls try this...Its Working....simple code
 Private Sub ComboBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox2.KeyPress
        ComboBox2.DroppedDown = True
        If Char.IsControl(e.KeyChar) Then Return
        With Me.ComboBox2
            Dim ToFind As String = .Text.Substring(0, .SelectionStart) & e.KeyChar
            Dim Index As Integer = .FindStringExact(ToFind)
 
            If Index = -1 Then Index = .FindString(ToFind)
            .SelectedIndex = Index
            .SelectionStart = ToFind.Length
            .SelectionLength = .Text.Length - .SelectionStart
            e.Handled = True
        End With
    End Sub

Open in new window