简体   繁体   中英

Enter key presses button

How do I get the enter key to press a button (when pressed in a text box)?

This is my code:

     private void bar_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            bargo.Click;
        }
    }

'bar' is the name of the textbox.

'bargo' is the name of the button.

You should add the error you're getting in your question, but it looks like it's an issue with the Click call rather than the Enter button. Try this

private void bar_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        ButtonHandler_Click(bargo,null);
    }
}

where ButtonHandler_Click is your button's Click event handler.

Better yet would be to call a method that ButtonHandler_Click also calls, rather than doing all the logic in the ButtonHandler_Click event handler.

还有另一种更简单的方法来实现这一点(尽管取决于您的情况):您可以将表单的AcceptButton属性设置为您想要按下的按钮,通常类似于“确定”或“提交”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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