Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 捕获windows窗体上的Enter键不工作_C#_.net_Winforms_Keypress - Fatal编程技术网

C# 捕获windows窗体上的Enter键不工作

C# 捕获windows窗体上的Enter键不工作,c#,.net,winforms,keypress,C#,.net,Winforms,Keypress,我有一个带有用户名和密码文本框的用户身份验证表单。有一个ok按钮,用于激发代码以验证凭据。 我希望在用户点击表单上的任意位置时执行相同的代码。 所以我像这样注册按键事件 this.KeyPress += UserLogin_KeyPress; private void UserLogin_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13)

我有一个带有用户名和密码文本框的用户身份验证表单。有一个
ok
按钮,用于激发代码以验证凭据。 我希望在用户点击表单上的任意位置时执行相同的代码。 所以我像这样注册按键事件

 this.KeyPress += UserLogin_KeyPress;
 private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
             if (e.KeyChar == (char)13)
                {
                MessageBox.Show("enter");
                }

        }
根本不会触发此事件。我做错了什么?

尝试以下操作:

private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyCode == Keys.Enter)
   {
      MessageBox.Show("enter");
   }
}
试试这个:

private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyCode == Keys.Enter)
   {
      MessageBox.Show("enter");
   }
}

它只是在看表格。表单上的控件也需要连接。

它只查看表单。表单上的控件也需要连接。

尝试将属性keypreview设置为true,并改为keydown,因为KeyPress不支持e。Keycode:

private void UserLogin_KeyPress(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            MessageBox.Show("Enter");
        }
    }

尝试将属性keypreview设置为true,并改为keydown,因为KeyPress不支持e。Keycode:

private void UserLogin_KeyPress(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            MessageBox.Show("Enter");
        }
    }


您是否已启用KeyPreview?。。。您可以尝试使用KeyDown事件来代替吗?如果(e.KeyCode==Keys.Enter)@GeraldoDiaz尝试过,它也不起作用。是否有原因设置不是有效的解决方案?@TnTinMn很酷。我不知道它。它工作得很好。谢谢。您是否启用了KeyPreview?。。。您可以尝试使用KeyDown事件来代替吗?如果(e.KeyCode==Keys.Enter)@GeraldoDiaz尝试过,它也不起作用。设置不是有效的解决方案有什么原因吗?@TnTinMn很酷。我不知道。它工作得很好。谢谢。
“KeyPressEventArgs”不包含“KeyCode”的定义。
尝试将AcceptButton属性设置为buttonName。
“KeyPressEventArgs”不包含“KeyCode”的定义。
尝试设置AcceptButton属性到buttonName。请为您的答案添加详细信息。请为您的答案添加详细信息。感谢您的努力。Keydown也不起作用。欢迎使用。您是否尝试过此。KeyPress+=新系统。Windows。Forms。KeyPress事件处理程序(此.frm\u KeyPress);True中的keyPreview属性如何?将keyPreview设置为
True
并更改为keydown解决了此问题。请更新您的答案。谢谢您的努力。keydown也不起作用。欢迎使用。您是否尝试过此方法。KeyPress+=new System.Windows.Forms.KeyPress EventHandler(此.frm\u KeyPress);True中的keyPreview属性如何?将keyPreview设置为
True
并更改为keydown解决了此问题。请更新您的答案。