vb.net 在按钮上按回车键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18585855/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:54:46  来源:igfitidea点击:

vb.net enter key press on a button

.netvb.net

提问by bharathivkmani

I have a form with a button and when i click TAB key the button got selected but when I click ENTER key this code is not executed.My code is,

我有一个带有按钮的表单,当我单击 TAB 键时,按钮被选中,但是当我单击 ENTER 键时,此代码不会执行。我的代码是,

Private Sub Button1_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress
If Asc(e.KeyChar) = 13 Then
 MsgBox("DO SOMETHING") 
End If 
End Sub 

回答by Nadeem_MK

I don't think you have anything to code.. If your focus is on a button and you press ENTER, the Button1_Clickevent will fire..

我不认为你有什么要编码的..如果你的焦点在一个按钮上并且你按下回车键,Button1_Click事件就会触发..

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox("DO SOMETHING")
End Sub

That's the case if your focus is on the button. And if ever you wish to fire your event wherever your focus is, the your code should be fitted in the Form1_KeyPress() event

如果您的注意力集中在按钮上,就是这种情况。如果您希望在您的焦点所在的任何地方触发您的事件,您的代码应该安装在 Form1_KeyPress() 事件中

回答by Lab Tipp

I found solution from this

我找到了解决方案,从

Code :

代码 :

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SendKeys.Send("{ENTER}")
End Sub

it work for me.

它对我有用。