0 votes
1.8k views
by (1.5k points)
I have to create a combobox that will accept user input (of course NOT dropdown list mode). However, input must be limited upto words in list items only, not any other character. Means it should not stop on word other than present in item list. Same time it should accept backspace and enter key.

1 Answer

0 votes
by (10.3k points)

Yea following code snippet will allow you to control the words typed in ComboBox box. It will allow the word only from a list box.

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
        If e.KeyChar = ControlChars.Back AndAlso e.KeyChar = ControlChars.Back Or Val(ComboBox1.Text) >= 1 Then
            Return
        End If

        Dim t As String = ComboBox1.Text
        Dim typedT As String = t.Substring(0, ComboBox1.SelectionStart)
        Dim newT As String = typedT + e.KeyChar

        Dim i As Integer = ComboBox1.FindString(newT)
        If i = -1 Then
            e.Handled = True
        End If
End Sub
...