vb.net 在 KeyDown 上捕获 keys.TAB

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

Capture keys.TAB on KeyDown

vb.nettabskeydownonkeydown

提问by Kartik Goyal

I am trying to capture TAB keypress on Keydown Event. I can see another post on How to fire an event when the tab key is pressed in a textbox?

我正在尝试在 Keydown 事件上捕获 TAB 按键。我可以看到另一篇关于如何在文本框中按下 Tab 键时触发事件的帖子

However, On the above link, posted solution is not working for me which I mentioned below.

但是,在上面的链接上,发布的解决方案对我不起作用,我在下面提到。

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
                         Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Tab Then
       e.SuppressKeyPress = True
       'do something
    End If
End Sub

For the testing purpose, I have added 2 simple textboxes on FORM1 and write the below code to capture the TAB on KeyDown event.

出于测试目的,我在 FORM1 上添加了 2 个简单的文本框,并编写了以下代码来捕获 KeyDown 事件上的 TAB。

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = True
        MsgBox("TAB DOWN")
    End If
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Me.Text = e.KeyChar
End Sub

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    If e.KeyCode = Keys.Tab Then
        MsgBox("TAB UP")
    End If
End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    Me.Text = "LEAVE"
End Sub

My above code should suppose to display a message box on KeyDown when TAB is press. It's not working.

我上面的代码应该假设在按下 TAB 时在 KeyDown 上显示一个消息框。它不起作用。

Please let me know what I am doing wrong. Thanks in advance!!!

请让我知道我做错了什么。提前致谢!!!

回答by Kartik Goyal

I found a new event called PreviewKeyDown()

我发现了一个名为 PreviewKeyDown() 的新事件

 Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Tab Then
        Me.Text = "TAB Capture From TextBox1_KeyDown At " & Now.ToString
    End If
End Sub

Private Sub TextBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown
    If e.KeyCode = Keys.Tab Then
        Me.Text = "TAB Capture From TextBox1_PreviewKeyDown At " & Now.ToString
    End If
End Sub

If you will execute the above code, you will able to capture TAB key on PreviewKeyDown() event.

如果您将执行上述代码,您将能够在 PreviewKeyDown() 事件上捕获 TAB 键。

回答by Karl Anderson

MsgBox()is a holdover from VB6 and you should use the .NET implementation of a message box, like this:

MsgBox()是 VB6 的保留,您应该使用消息框的 .NET 实现,如下所示:

MessageBox.Show("TAB UP")

Also, you are setting a Textproperty against the instance of the form class (Me), when I think you intend to set the Textproperty of the text box, like this:

此外,您正在Text针对表单类 ( Me)的实例设置一个属性,当我认为您打算设置Text文本框的属性时,如下所示:

Me.TextBox1.Text = e.KeyChar