如何通过传递参数来调用 keyDown 事件,Winforms Vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27120669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I call keyDown event by passing arguments, Winforms Vb.net
提问by user1777733
The Following is a combo box keydown event
以下是组合框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
I would like to trigger same event from combobox_leave by passing 'enter key' I did as follows but not working, how to achieve this?
我想通过传递“输入键”来触发来自 combobox_leave 的相同事件我做了如下但不起作用,如何实现?
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1_KeyDown(Me, Keys.Enter)
End Sub
采纳答案by DevEstacion
Why not just extract the method from the actual event?
为什么不直接从实际事件中提取方法呢?
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
回答by Myk Agustin
You could also use the SendKeys.SendMethod
您还可以使用SendKeys.Send方法
When the user leaves the Combobox (like in your example),
当用户离开组合框时(如您的示例),
You could set back the Focus to the combobox
您可以将焦点设置回组合框
and then use SendKeys.Send("{ENTER}")to trigger the enter keydown.
然后用于SendKeys.Send("{ENTER}")触发回车键。
much like this:
很像这样:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Focus()
SendKeys.Send("{ENTER}")
End Sub
However this prevents users from focusing to another component. To prevent this, you could use an if statementthat if the user clicks or focuses on another component after focusing on the combobox, the user can still "leave" the combobox.
然而,这会阻止用户专注于另一个组件。为了防止这种情况,您可以使用if statementthat 如果用户在关注组合框后单击或关注另一个组件,用户仍然可以“离开”组合框。
Your kind of approach is not advisable and this leads to a misunderstanding in the part of the user.
您的这种方法是不可取的,这会导致用户产生误解。
回答by Arrod
try this :
尝试这个 :
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

