How can I capture enter keypresses anywhere on my form and force it to fire the submit button event?
如何在表单上的任何位置捕获输入按键并强制它触发提交按钮事件?
9 个解决方案
#1
167
If you set your Form's AcceptButton property to one of the Buttons on the Form, you'll get that behaviour by default.
如果将Form的AcceptButton属性设置为表单上的某个按钮,则默认情况下会获得该行为。
Otherwise, set the KeyPreview property to True on the Form and handle its KeyDown event. You can check for the Enter key and take the necessary action.
否则,在Form上将KeyPreview属性设置为True并处理其KeyDown事件。您可以检查Enter键并采取必要的操作。
#2
20
You can designate a button as the "AcceptButton" in the Form's properties and that will catch any "Enter" keypresses on the form and route them to that control.
您可以在Form的属性中将按钮指定为“AcceptButton”,它将捕获表单上的任何“Enter”按键并将它们路由到该控件。
See this MSDN article and note the few exceptions it outlines (multi-line text-boxes, etc.)
请参阅此MSDN文章,并注意它概述的少数例外情况(多行文本框等)
#3
14
private void textBox_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter){
button.PerformClick();
}
}
#4
8
As previously stated, set your form's AcceptButton property to one of its buttons AND set the DialogResult property for that button to DialogResult.OK, in order for the caller to know if the dialog was accepted or dismissed.
如前所述,将表单的AcceptButton属性设置为其按钮之一并将该按钮的DialogResult属性设置为DialogResult.OK,以便调用者知道对话框是被接受还是被解除。
#5
6
You can subscribe to the KeyUp event of the text box.
您可以订阅文本框的KeyUp事件。
private void txtInput_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
DoSomething();
}
#6
2
The Form has a KeyPreview property that you can use to intercept the keypress.
Form具有KeyPreview属性,您可以使用它来拦截按键。
#7
0
Set the KeyPreview attribute on your form to True, then use the KeyPress event at your form level to detect the Enter key. On detection call whatever code you would have for the "submit" button.
将表单上的KeyPreview属性设置为True,然后使用表单级别的KeyPress事件来检测Enter键。检测时,调用“提交”按钮所需的代码。
#8
0
if (e.KeyCode.ToString() == "Return")
{
//do something
}
#9
0
Simply use
this.Form.DefaultButton = MyButton.UniqueID;
**Put your button id in place of 'MyButton'.
**将您的按钮ID替换为“MyButton”。
#1
167
If you set your Form's AcceptButton property to one of the Buttons on the Form, you'll get that behaviour by default.
如果将Form的AcceptButton属性设置为表单上的某个按钮,则默认情况下会获得该行为。
Otherwise, set the KeyPreview property to True on the Form and handle its KeyDown event. You can check for the Enter key and take the necessary action.
否则,在Form上将KeyPreview属性设置为True并处理其KeyDown事件。您可以检查Enter键并采取必要的操作。
#2
20
You can designate a button as the "AcceptButton" in the Form's properties and that will catch any "Enter" keypresses on the form and route them to that control.
您可以在Form的属性中将按钮指定为“AcceptButton”,它将捕获表单上的任何“Enter”按键并将它们路由到该控件。
See this MSDN article and note the few exceptions it outlines (multi-line text-boxes, etc.)
请参阅此MSDN文章,并注意它概述的少数例外情况(多行文本框等)
#3
14
private void textBox_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter){
button.PerformClick();
}
}
#4
8
As previously stated, set your form's AcceptButton property to one of its buttons AND set the DialogResult property for that button to DialogResult.OK, in order for the caller to know if the dialog was accepted or dismissed.
如前所述,将表单的AcceptButton属性设置为其按钮之一并将该按钮的DialogResult属性设置为DialogResult.OK,以便调用者知道对话框是被接受还是被解除。
#5
6
You can subscribe to the KeyUp event of the text box.
您可以订阅文本框的KeyUp事件。
private void txtInput_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
DoSomething();
}
#6
2
The Form has a KeyPreview property that you can use to intercept the keypress.
Form具有KeyPreview属性,您可以使用它来拦截按键。
#7
0
Set the KeyPreview attribute on your form to True, then use the KeyPress event at your form level to detect the Enter key. On detection call whatever code you would have for the "submit" button.
将表单上的KeyPreview属性设置为True,然后使用表单级别的KeyPress事件来检测Enter键。检测时,调用“提交”按钮所需的代码。
#8
0
if (e.KeyCode.ToString() == "Return")
{
//do something
}
#9
0
Simply use
this.Form.DefaultButton = MyButton.UniqueID;
**Put your button id in place of 'MyButton'.
**将您的按钮ID替换为“MyButton”。