C#我如何通过按下回车键,而文本框具有焦点点击一个按钮? [英] C# How do I click a button by hitting Enter whilst textbox has focus?
本文介绍了C#我如何通过按下回车键,而文本框具有焦点点击一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#中的winform应用程序的工作,之后我在一个文本框输入一些东西我想打回车键,但文本框中仍具有焦点(闪烁的光标仍处于文本框),我怎么能做到这一点?
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?
推荐答案
简单的选项只是形式的的AcceptButton设置为你想要pressed按钮(通常是OK等):
The simple option is just to set the forms's AcceptButton to the button you want pressed (usually "OK" etc):
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 } });
如果这不是一种选择,你可以看看KeyDown事件等,但更多的工作...
If this isn't an option, you can look at the KeyDown event etc, but that is more work...
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 } });
这篇关于C#我如何通过按下回车键,而文本框具有焦点点击一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文