简体   繁体   English

输入键按下按钮

[英]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. 'bar' 是文本框的名称。

'bargo' is the name of the button. 'bargo' 是按钮的名称。

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.您应该添加您在问题中遇到的错误,但看起来这是Click调用而不是Enter按钮的问题。 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.其中ButtonHandler_Click是按钮的Click事件处理程序。

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.更好的是调用ButtonHandler_Click也调用的方法,而不是在ButtonHandler_Click事件处理程序中执行所有逻辑。

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

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

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