why do I have to press escape on the keyboard twice in vb.net

77 Views Asked by At

I'm using the DropdownStyle Combobox: Dropdown

If I press on the keyboard with enter then only once but why if I press on the keyboard with escape then it must be done twice,

I use the String.Equals Method to check the value selected in the combox is correct or not by referring to the textbox.

why do I have to press escape on the keyboard twice?

or is there another method that I have to do

Is there something wrong with my code?

Please guide me

Thanks

Public Class Form4
    Private count As Integer = 1
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        If keyData.ToString() = "Escape" Then
            If count = 1 Then
                count += 1
                Label1.Text = "You are pressing key escape once"
            ElseIf count = 2 Then
                Label1.Text = "You are pressing key escape twice"
            End If

        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
 Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
    End Sub
    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.AutoCompleteCustomSource = AutoSuggestList()
    End Sub
    Private Function AutoSuggestList() As AutoCompleteStringCollection
        Dim collection As New AutoCompleteStringCollection
        Using connection As New OleDbConnection(GetOledbConnectionString())
            collection.AddRange(connection.Query(Of String)("SELECT ColorCode FROM ColorCode").ToArray)
        End Using
        Return collection
    End Function

    Private Sub GetItemData(ByVal iditem As String)
        Dim item = COLORCODESERVICE.GetByColorcode2(iditem)
        If item IsNot Nothing Then
            If String.Equals(iditem, item.Colorcode, StringComparison.CurrentCultureIgnoreCase) Then
                ComboBox1.Text = item.Colorcode
            End If
            TextBox1.Text = item.Colorname
        Else
            MsgBox("Not Found Colorcode...")
            ComboBox1.ResetText()
            TextBox1.Clear()
            Return
        End If
    End Sub
 Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
        If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Escape Then
            GetItemData(ComboBox1.Text)
        ElseIf e.KeyCode = Keys.Back Then
            ComboBox1.ResetText()
            TextBox1.Clear()
        End If
    End Sub
End Class

result press ecape twice

1

There are 1 best solutions below

3
dbasnett On

Typically you use a ComboBox as way to select a valid value.

Compare this to what you have,

Public Class Form1

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Dim items() As String = {"one", "two", "three", "four", "five"} 'test items
        ComboBox1.DataSource = items 'change to your data
        ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems '<<<<<<<<<<<<<<<
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Debug.Write("SI ")
        TextBox1.Text = ComboBox1.SelectedValue.ToString
    End Sub

    Private Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
        'the user is typing in ComboBox
        'if they press enter over a valid entry the
        '  .SelectedIndexChanged event will fire
        Debug.WriteLine("TC ")
        TextBox1.Text = "" 'blank is invalid
    End Sub
End Class