C# KeyEvent 不记录 Enter/Return 键
我一直在用 C# 制作这个登录表单,我想在用户单击“提交”或按 Enter/Return 键后立即“提交”所有数据。
我已经用 KeyEvents 进行了一些测试,但到目前为止没有任何效果。
void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
上面的代码首先是为了测试该事件是否有效。 它工作得很好,当我按“d”时,它会显示“d”,当我按“8”时,它会显示“8”,但按 Enter 不会执行任何操作。
所以我认为这是因为 Enter 并没有真正绑定到一个字符,但它确实显示了退格键,它工作得很好,所以这让我很困惑为什么它没有注册我的 Enter 键。
所以问题是: 如何记录输入/返回键?为什么它不立即记录按键操作?
注意:我已将事件放入文本框中,
tbPassword.KeyPress += new KeyPressEventHandler(tbPassword_KeyPress);
因此当选择文本框时按下 Enter 按钮时会触发该事件(这是当然,整个过程)也许这与代码的执行有关。
I've been making this login form in C# and I wanted to 'submit' all the data as soon as the user either clicks on submit or presses the enter/return key.
I've been testing a bit with KeyEvents but nothing so far worked.
void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
The above code was to test if the event even worked in the first place.
It works perfectly, when I press 'd' it shows me 'd' when I press '8' it shows me '8' but pressing enter doesn't do anything.
So I though this was because enter isn't really bound to a character but it did show backspace, it worked just fine so it got me confused about why it didn't register my enter key.
So the question is:
How do I log the enter/return key? and why doesn't it log the key press right right now like it should?
note: I've put the event in a textbox
tbPassword.KeyPress += new KeyPressEventHandler(tbPassword_KeyPress);
So it fires when the enter button is pressed WHILE the textbox is selected (which is was the whole time of course) maybe that has something to do with the execution of the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是否有定义为默认操作的按钮?
如果是这样,那么该控件将吞噬 Enter 键。
也许这就是你的答案。您需要将提交按钮上的 DefaultAction 属性设置为 true。
Do you have a button defined as the default action?
If so then that control will gobble up the Enter key.
And maybe that is your answer. You need to set the DefaultAction property to true on your submit button.
请尝试使用 KeyDown 事件。
Try the KeyDown event instead.
也许您应该使用表单的“AcceptButton”将其设置为提交按钮。认为这就是你真正的...
Perhaps you should use the "AcceptButton" of the form to set it to the submit button. Think that is what you what really...
您遗漏了重要的一点,您必须根据条件将
Handled
属性设置为 true 或 false...You have left out a vital bit, you must set the
Handled
property to true or false depending on the condition...试试这个
Try this
转到您的表单...
在基本表单中更改此
FormName.AcceptButton = buttonName;
这将自动读取输入...的按键日志文件..
如果您不希望用户看到接受按钮
buttonName.Visible = false;, 则可以执行此操作
FormName.AcceptButton = 按钮名称;
AcceptButton自动读取键盘上的回车键
go to your forms...
in the basic form change this
FormName.AcceptButton = buttonName;
this would read the key log file of enter... automatically..
you can do this if you dont want users to see accept button
buttonName.Visible = false;
FormName.AcceptButton = buttonName;
AcceptButton automatically reads the enter key from the keyboard