我正在使用 DropdownStyle Combobox:Dropdown
如果我按 Enter 键盘,那么只按一次,但为什么如果我按 Escape 键盘,则必须执行两次,
我使用 String.Equals 方法通过参考文本框来检查组合框中选择的值是否正确。
为什么我必须按两次键盘上的 Esc 键?
或者我还有其他方法吗
我的代码有问题吗?
请指导我
谢谢
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
通常您使用组合框来选择有效值。
将此与您拥有的进行比较,
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 = "" 'blsnk is invalid
End Sub
End Class