[英]Capturing Enter Key press on windows form not working
I have a user authentication form with username and password textboxes.There is an okay
button that fires the code to validate the credentials. 我有一个带有用户名和密码文本框的用户身份验证表单。有一个
okay
按钮可以触发代码以验证凭据。 I want the same code to get executed when the user hits Enter
key anywhere on the form. 我希望当用户在窗体上的任何地方单击
Enter
键时执行相同的代码。 So i register for the keypress event like this 所以我注册了这样的按键事件
this.KeyPress += UserLogin_KeyPress;
private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
MessageBox.Show("enter");
}
}
This event is not triggered at all.What i'm i doing wrong? 此事件根本不会触发。我在做什么错?
Try setting the property keypreview to true and changing to keydown instead since KeyPress don't support e.Keycode: 尝试将属性keypreview设置为true,然后更改为keydown,因为KeyPress不支持e.Keycode:
private void UserLogin_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter");
}
}
Try this: 尝试这个:
private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("enter");
}
}
It's only looking at the form. 只看表格。 Controls on the form also need to be wired in.
表单上的控件也需要连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.