C# 如何在文本框具有焦点时按 Enter 键单击按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/299086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# How do I click a button by hitting Enter whilst textbox has focus?
提问by
I'm working with a WinForm app in C#, after I type something in a textbox I want to hit the Enter key but the textbox still has focus (flashing cursor is still in textbox), how can I achieve this?
我正在使用 C# 中的 WinForm 应用程序,在文本框中键入内容后,我想按 Enter 键,但文本框仍然具有焦点(闪烁的光标仍在文本框中),我该如何实现?
回答by Marc Gravell
The simple option is just to set the forms's AcceptButton to the button you want pressed (usually "OK" etc):
简单的选项就是将表单的 AcceptButton 设置为您想要按下的按钮(通常是“OK”等):
TextBox tb = new TextBox();
Button btn = new Button { Dock = DockStyle.Bottom };
btn.Click += delegate { Debug.WriteLine("Submit: " + tb.Text); };
Application.Run(new Form { AcceptButton = btn, Controls = { tb, btn } });
If this isn't an option, you can look at the KeyDown event etc, but that is more work...
如果这不是一个选项,您可以查看 KeyDown 事件等,但这需要更多工作...
TextBox tb = new TextBox();
Button btn = new Button { Dock = DockStyle.Bottom };
btn.Click += delegate { Debug.WriteLine("Submit: " + tb.Text); };
tb.KeyDown += (sender,args) => {
if (args.KeyCode == Keys.Return)
{
btn.PerformClick();
}
};
Application.Run(new Form { Controls = { tb, btn } });
回答by Jon Norton
The usual way to do this is to set the Form
's AcceptButton
to the button you want "clicked". You can do this either in the VS designer or in code and the AcceptButton
can be changed at any time.
执行此操作的常用方法是将Form
's设置为AcceptButton
您想要“单击”的按钮。您可以在 VS 设计器或代码中执行此操作,并且AcceptButton
可以随时更改。
This may or may not be applicable to your situation, but I have used this in conjunction with GotFocus
events for different TextBox
es on my form to enable different behavior based on where the user hit Enter. For example:
这可能适用于您的情况,也可能不适用,但我已将其与表单上GotFocus
不同TextBox
es 的事件结合使用,以根据用户按 Enter 的位置启用不同的行为。例如:
void TextBox1_GotFocus(object sender, EventArgs e)
{
this.AcceptButton = ProcessTextBox1;
}
void TextBox2_GotFocus(object sender, EventArgs e)
{
this.AcceptButton = ProcessTextBox2;
}
One thing to be careful of when using this method is that you don't leave the AcceptButton
set to ProcessTextBox1
when TextBox3
becomes focused. I would recommend using either the LostFocus
event on the TextBox
es that set the AcceptButton
, or create a GotFocus
method that all of the controls that don't use a specific AcceptButton
call.
有一两件事要小心使用这种方法时,就是你不要离开AcceptButton
设定到ProcessTextBox1
时TextBox3
变得集中。我建议LostFocus
在TextBox
设置的es上使用事件AcceptButton
,或者创建一个GotFocus
方法,所有不使用特定AcceptButton
调用的控件。
回答by user344760
YOu can trap it on the keyup event http://www.itjungles.com/dotnet/c-how-to-easily-detect-enter-key-in-textbox-and-execute-a-method
你可以在 keyup 事件中捕获它http://www.itjungles.com/dotnet/c-how-to-easily-detect-enter-key-in-textbox-and-execute-a-method
回答by r3st0r3
This is very much valid for WinForms. However, in WPF you need to do things differently, and it is easer. Set the IsDefaultproperty of the Buttonrelevant to this text-area as true.
这对 WinForms 非常有效。但是,在 WPF 中,您需要以不同的方式做事,而且更容易。将与此文本区域相关的Button的IsDefault属性设置为 true。
Once you are done capturing the enter key, do not forget to toggle the properties accordingly.
完成捕获回车键后,不要忘记相应地切换属性。
回答by RJ Lohan
I came across this whilst looking for the same thing myself, and what I note is that none of the listed answers actually provide a solution when you don't want to click the 'AcceptButton' on a Form when hitting enter.
我在自己寻找同样的事情时遇到了这个问题,我注意到当您不想在按 Enter 键时单击表单上的“接受按钮”时,列出的答案实际上都没有提供解决方案。
A simple use-case would be a text search box on a screen where pressing enter should 'click' the 'Search' button, not execute the Form's AcceptButton behaviour.
一个简单的用例是屏幕上的文本搜索框,其中按 Enter 应该“单击”“搜索”按钮,而不是执行表单的 AcceptButton 行为。
This little snippet will do the trick;
这个小片段可以解决问题;
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
if (!textBox.AcceptsReturn)
{
button1.PerformClick();
}
}
}
In my case, this code is part of a custom UserControl derived from TextBox, and the control has a 'ClickThisButtonOnEnter' property. But the above is a more general solution.
就我而言,此代码是从 TextBox 派生的自定义 UserControl 的一部分,并且该控件具有“ClickThisButtonOnEnter”属性。但以上是更通用的解决方案。
回答by ruvi
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button.PerformClick();
// these last two lines will stop the beep sound
e.SuppressKeyPress = true;
e.Handled = true;
}
}
Bind this KeyDown event to the textbox, then when ever you press a key, this event will be fired. Inside the event we check whether user has pressed "Enter key", if so, you can perform you action
将此 KeyDown 事件绑定到文本框,然后当您按下某个键时,将触发此事件。在事件内部,我们检查用户是否按下了“Enter 键”,如果是,您可以执行您的操作
回答by philx_x
Most beginner friendly solution is:
最适合初学者的解决方案是:
In your Designer, click on the text field you want this to happen. At the properties Window (default: bottom-right) click on the thunderbolt (Events). This icon is next to the alphabetical sort icon and the properties icon.
Scroll down to
keyDown
. Click on the Dropdown field right to it. You'll notice there's nothing in there so simply press enter. Visual Studio will write you the following code:private void yourNameOfTextbox_KeyDown(object sender, KeyEventArgs e) { }
Then simply paste this between the brackets:
if (e.KeyCode == Keys.Enter) { yourNameOfButton.PerformClick(); }
在您的设计器中,单击您希望发生这种情况的文本字段。在属性窗口(默认:右下角)点击雷电(事件)。此图标位于字母排序图标和属性图标旁边。
向下滚动到
keyDown
。单击它右侧的下拉字段。你会注意到里面什么都没有,所以只需按回车键。Visual Studio 将为您编写以下代码:private void yourNameOfTextbox_KeyDown(object sender, KeyEventArgs e) { }
然后只需将其粘贴在括号之间:
if (e.KeyCode == Keys.Enter) { yourNameOfButton.PerformClick(); }
This will act as you would have clicked it.
这将像您单击它一样。
回答by user5841303
Simply set form "Accept Button" Property to button that you want to hit by Enter key.
Or in load event write this.acceptbutton = btnName;
只需将表单“接受按钮”属性设置为您想要按 Enter 键点击的按钮。或者在加载事件中写入this.acceptbutton = btnName;
回答by Michael
Add this to the Form's constructor:
将此添加到 Form 的构造函数中:
this.textboxName.KeyDown += (sender, args) => {
if (args.KeyCode == Keys.Return)
{
buttonName.PerformClick();
}
};
回答by jake mariano
Or you can just use this simple 2 liner code :)
或者你可以只使用这个简单的 2 行代码:)
if (e.KeyCode == Keys.Enter)
button1.PerformClick();