在我的 Visual Studio 2010 的 winforms 应用程序中,
我有一个按钮和两个组合框(combobox1
,combobox2
)。
我在按钮中添加了代码以清除第一个组合框(combobox1)中先前输入的数据并将焦点设置为它。
在第一个组合框(combobox1
)的 keyup 事件中,我检查了输入键,如果按下,焦点将移动到下一个组合框(combobox2
)。
但我的问题是,当我按下按钮时(通过在焦点位于按钮上时按下回车键),焦点直接移动到最后一个(第二个)组合框(combobox2
)。
尽管我只在按钮上输入了组合框 1 的 Keyup 事件,但它会自动触发
我该如何解决这个问题?
我的代码在下面给出
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'code to perform database action
Me.ComboBox1.Focus()
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
If e.KeyCode = Keys.Enter Then
If Me.ComboBox1.SelectedText = String.Empty Then
ComboBox2.Focus()
End If
End If
End Sub
更新
当我单击按钮时,不会出现问题但是当我在焦点位于按钮上时按 Enter 键以触发按钮单击事件时,会出现问题。