简体   繁体   English

在keyDown事件中按下Enter键后如何处理命令

[英]How to process the command after pressing Enter in keyDown event for button

I'm writing an application controlled only by numblock on keyboard and I need to solve the problem with this code: 我正在编写仅由键盘上的numblock控制的应用程序,我需要用以下代码解决问题:

private void btnSaveCorrection_KeyDown(object sender, KeyEventArgs e)
{
    MessageBox.Show("HEY");

    if (e.KeyCode == Keys.Enter)
    {
        MessageBox.Show("HEY");
        // Save to DB and jump to next control
    }
    else
    {
        MessageBox.Show("WTF");
        // if e.KeyCode is [+] or [-], jump to next/previous control
    }
}

The problem is: When I press Enter, nothing happens. 问题是:当我按Enter键时,什么也没发生。 When I press any other key, MessageBox "WTF" is showed. 当我按任何其他键时,将显示消息框“ WTF”。 Why is all code in KeyDown event ignored when the Enter is pressed? 为什么按下Enter键时会忽略KeyDown事件中的所有代码? How to fix it? 如何解决?

Thanks for every reply. 感谢您的每一个答复。

Well, for me it works if you link the KeyUp event instead of the KeyDown Event. 好吧,对我来说,如果您链​​接KeyUp事件而不是KeyDown事件,它就可以工作。

I think the problem is that the enter key is considered a "click" on the button as its default behavior. 我认为问题在于回车键被视为按钮的“单击”行为,这是其默认行为。

My advice is link the KeyUp event instead of the KeyDown event. 我的建议是链接KeyUp事件而不是KeyDown事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM