在 VB.NET 上检测 Enter 按键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2752424/
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
Detecting Enter keypress on VB.NET
提问by Tareq
I am using .NET 3.5 framework of VB.NET 2008.
我正在使用 VB.NET 2008 的 .NET 3.5 框架。
I have some textboxes in my form. I want the tab-like behavior when my user presses ENTER on one of my textboxes. I used the following code:
我的表单中有一些文本框。当我的用户在我的文本框之一上按下 ENTER 时,我想要类似选项卡的行为。我使用了以下代码:
Private Sub txtDiscount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDiscount.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
But it doesn't work for me.
但这对我不起作用。
What is the solution?
解决办法是什么?
采纳答案by Tareq
There is no need to set the KeyPreview Property to True. Just add the following function.
无需将 KeyPreview 属性设置为 True。只需添加以下功能。
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys) _
As Boolean
If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
SendKeys.Send("{Tab}")
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Now, when you press Enter on a TextBox, the control moves to the next control.
现在,当您在 TextBox 上按 Enter 键时,控件将移动到下一个控件。
回答by Dinidu Hewage
In the KeyDown Event:
在 KeyDown 事件中:
If e.KeyCode = Keys.Enter Then
Messagebox.Show("Enter key pressed")
end if
回答by Eric Schneider
Make sure the form KeyPreview property is set to true.
确保表单 KeyPreview 属性设置为 true。
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
回答by EConway
I'm using VB 2010 .NET 4.0 and use the following:
我正在使用 VB 2010 .NET 4.0 并使用以下内容:
Private Sub tbSecurity_KeyPress(sender As System.Object, e As System.EventArgs) Handles tbSecurity.KeyPress
Dim tmp As System.Windows.Forms.KeyPressEventArgs = e
If tmp.KeyChar = ChrW(Keys.Enter) Then
MessageBox.Show("Enter key")
Else
MessageBox.Show(tmp.KeyChar)
End If
End Sub
Works like a charm!
奇迹般有效!
回答by IT Vlogs
You can use PreviewKeyDown Event
您可以使用 PreviewKeyDown 事件
Private Sub txtPassword_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles txtPassword.PreviewKeyDown
If e.KeyCode = Keys.Enter Then
Call btnLogin_Click(sender, e)
End If
End Sub
Tested on VB.NET 2010
在 VB.NET 2010 上测试
回答by Ika
also can try this:
也可以试试这个:
If e.KeyChar = ChrW(Keys.Enter) Then
'Do Necessary code here
End If
回答by halfacreSal
I see this has been answered, but it seems like you could avoid all of this 'remapping' of the enter key by simply hooking your validation into the AcceptButton on a form. ie. you have 3 textboxes (txtA,txtB,txtC) and an 'OK' button set to be AcceptButton (and TabOrder set properly). So, if in txtA and you hit enter, if the data is invalid, your focus will stay in txtA, but if it is valid, assuming the other txts need input, validation will just put you into the next txt that needs valid input thus simulating TAB behaviour... once all txts have valid input, pressing enter will fire a succsessful validation and close form (or whatever...) Make sense?
我看到这已经得到了回答,但似乎您可以通过简单地将您的验证连接到表单上的 AcceptButton 来避免所有这些输入键的“重新映射”。IE。您有 3 个文本框(txtA、txtB、txtC)和一个“确定”按钮设置为 AcceptButton(并且 TabOrder 设置正确)。所以,如果在 txtA 中你按下回车,如果数据无效,你的焦点将留在 txtA,但如果它是有效的,假设其他 txt 需要输入,验证只会让你进入下一个需要有效输入的 txt,因此模拟 TAB 行为...一旦所有 txt 都有有效输入,按 Enter 键将触发成功验证并关闭表单(或其他任何...)有意义吗?
回答by Alexander Malygin
Private Sub SomeTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles SomeTextBox.KeyPress
If Asc(e.KeyChar) = 13 Then
MessageBox.Show("Enter pressed!")
e.Handled = True
End If
End Sub
回答by Herbert Yeo
Use KeyDown Event instead of KeyPress
使用 KeyDown 事件代替 KeyPress
If e.KeyCode = Keys.Enter Then
MsgBox ("You pressed enter")
End if
Note: Make sure you don't have ACCEPT BUTTON set on your form. AcceptButton set it to 'none'
注意:确保您没有在表单上设置接受按钮。AcceptButton 将其设置为“无”
回答by Adnan Zaheer
I have test this code on Visual Studio 2019
我已在 Visual Studio 2019 上测试过此代码
Its working superb
它的工作精湛
Just Paste it into you form code
只需将其粘贴到您的表单代码中
it will work on all textboxs on same form
它将适用于相同形式的所有文本框
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Dim keyCode As Keys = CType(msg.WParam, IntPtr).ToInt32
Const WM_KEYDOWN As Integer = &H100
If msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter _
AndAlso Me.ActiveControl.GetType.Name = "TextBox" Then
Me.SelectNextControl(Me.ActiveControl, True, True, False, True)
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function