0

I have a button that keeps stealing my keypress event. How do I override this? I want to have a method run when enter is clicked when the focus is on the form, but the button steals it.

4

2 回答 2

0

处理 Form 中的 KeyDown 事件:

private void Form1_KeyDown(Object sender, KeyEventArgs e)
{
    if ((e.KeyCode & Keys.Enter) == Keys.Enter)
    {
        //do something
        e.Handled = true;
    }
    else
        base.OnKeyDown(e);
}
于 2013-06-04T17:59:58.113 回答
0

Buttons不要窃取事件,他们“只”窃取密钥。所以你可以做这样的事情:KeyPressEnter

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Enter)
    {
        processYourKey( keyData);
        return false;                 // do not steal!
    }
    processYourKey( keyData);
    return base.ProcessCmdKey(ref msg, keyData);
}
于 2016-01-25T11:03:38.177 回答