Tuesday, September 27, 2011

combobox autocomplete - DROPDOWNLIST

Bernie,

To give me the idea, can you try if this is what you mean, you have to set
the combobox to the normal dropdown however set that dropdownliststyle to
true. I did not test it real deep, hower got the idea that it was working.

\\\
Private DropDownListStyle As Boolean
Private Sub combobox1_KeyUp(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
        Dim cbo As ComboBox = DirectCast(sender, ComboBox)
        Select Case e.KeyCode
            Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete,
Keys.Down
                Return
        End Select
        Dim ComboBoxText As String = cbo.Text
        Dim FirstFound As Integer = cbo.FindString(ComboBoxText)
        If FirstFound >= 0 Then
            Dim FirstFoundItem As Object = cbo.Items(FirstFound)
            Dim FirstFoundText As String = cbo.GetItemText(FirstFoundItem)
            cbo.Text = FirstFoundText
            cbo.SelectionStart = ComboBoxText.Length
            cbo.SelectionLength = cbo.Text.Length
        Else
            If DropDownListStyle = True Then
                cbo.Text = cbo.Text.Substring(0, cbo.Text.Length - 1)
                cbo.SelectionStart = cbo.Text.Length
            End If
        End If
End Sub
///

No comments: