C# KeyEvent 不记录 Enter/Return 键

发布于 2024-08-26 10:13:30 字数 651 浏览 8 评论 0原文

我一直在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

美胚控场 2024-09-02 10:13:30

您是否有定义为默认操作的按钮?

如果是这样,那么该控件将吞噬 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.

孤檠 2024-09-02 10:13:30

请尝试使用 KeyDown 事件。

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

Try the KeyDown event instead.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        MessageBox.Show("Enter");
    }
}
用心笑 2024-09-02 10:13:30

也许您应该使用表单的“AcceptButton”将其设置为提交按钮。认为这就是你真正的...

Perhaps you should use the "AcceptButton" of the form to set it to the submit button. Think that is what you what really...

假装爱人 2024-09-02 10:13:30

您遗漏了重要的一点,您必须根据条件将 Handled 属性设置为 true 或 false...

void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
{
    MessageBox.Show(e.KeyChar.ToString());
    if (e.KeyCode == Keys.Enter){
      // This is handled and will be removed from Windows message pump
      e.Handled = true; 
    }
}

You have left out a vital bit, you must set the Handled property to true or false depending on the condition...

void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
{
    MessageBox.Show(e.KeyChar.ToString());
    if (e.KeyCode == Keys.Enter){
      // This is handled and will be removed from Windows message pump
      e.Handled = true; 
    }
}
扬花落满肩 2024-09-02 10:13:30

试试这个

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
    {
        MessageBox.Show("Enter Key Pressed", "Enter Key Pressed", MessageBoxButtons.OK);
    }
}

Try this

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
    {
        MessageBox.Show("Enter Key Pressed", "Enter Key Pressed", MessageBoxButtons.OK);
    }
}
无声静候 2024-09-02 10:13:30

转到您的表单...

在基本表单中更改此

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文