以下是组合框keydown事件
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
我想通过传递'输入键'来从combobox_leave触发相同的事件。我做了如下但没有工作,如何实现这个目标?
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1_KeyDown(Me, Keys.Enter)
End Sub
答案 0 :(得分:0)
为什么不从实际事件中提取方法?
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
performAction(e.KeyCode);
End Sub
Private Sub performAction(e as Keys)
If e = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
performAction(Keys.Enter);
End Sub
答案 1 :(得分:0)
您还可以使用SendKeys.Send方法
当用户离开Combobox时(如您的示例所示),
您可以将Focus设置回组合框
然后使用SendKeys.Send("{ENTER}")
触发enter keydown。
很像这样:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Focus()
SendKeys.Send("{ENTER}")
End Sub
然而,这会阻止用户专注于其他组件。为防止出现这种情况,您可以使用if statement
,如果用户在关注组合框后点击或关注其他组件,则用户仍然可以离开"组合框。
您的方法不可取,这会导致用户方面的误解。
答案 2 :(得分:0)
试试这个:
Private Sub ComboBox1_KeyDown(sender As Object, e As
keyEventArgs) Handles ComboBox1.KeyDown
Dim _KeyCode As Short
If e Is Nothing Then
_KeyCode = 13
Else
_KeyCode = Keys.Enter
End If
If _KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs)
Handles ComboBox1.Leave
Dim keypress As System.Windows.Forms.KeyPressEventArgs
ComboBox1_KeyDown(sender, keypress)
End Sub