在 VB.NET 上检测 Enter 按键 [英] Detecting Enter keypress on VB.NET

查看:28
本文介绍了在 VB.NET 上检测 Enter 按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 VB.NET 2008 的 .NET 3.5 框架.

I am using .NET 3.5 framework of VB.NET 2008.

我的表单中有一些文本框.当我的用户在我的文本框之一上按下 ENTER 时,我想要类似选项卡的行为.我使用了以下代码:

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:

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

但它对我不起作用.

解决方案是什么?

推荐答案

无需将 KeyPreview 属性设置为 True.只需添加以下功能即可.

There is no need to set the KeyPreview Property to True. Just add the following function.

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

现在,当您在 TextBox 上按 Enter 键时,控件将移动到下一个控件.

Now, when you press Enter on a TextBox, the control moves to the next control.

这篇关于在 VB.NET 上检测 Enter 按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆