C# 在 Windows 窗体中点击默认按钮(试图找到最佳解决方案)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/446147/
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
Default button hit in windows forms (trying to find the best solution)
提问by 0x49D1
The question is: How to make default button be focused on form focus and response on "Enter" hit, but not focused when the caret is in textbox with multiline property set to true for example?..I know that i can do some exceptions in code, but maybe there is some "best practices" i dont know for now :( thank you
问题是:例如,如何使默认按钮专注于表单焦点和对“Enter”命中的响应,但当插入符号位于文本框中且多行属性设置为 true 时不聚焦?...我知道我可以做一些例外在代码中,但也许有一些我现在不知道的“最佳实践”:(谢谢
采纳答案by Xn0vv3r
Maybe I got you wrong, but what I would do is:
也许我误会了你,但我会做的是:
- Set the "AcceptButton" of the form to the Button you want to respond on "Enter"
- Set the "AcceptsReturn" of the textbox with multiline to true
- 将表单的“AcceptButton”设置为要在“Enter”时响应的按钮
- 将多行文本框的“AcceptsReturn”设置为true
et voila
等等
回答by Rune Grimstad
A Windows Form has two properties: AcceptButton and CancelButton. You can set these to refer button controls on your form. The AcceptButton tells which button should be clicked when the user presses the enter key, while the Cancel button tells which button should be clicked when the user presses the escape key.
Windows 窗体有两个属性:AcceptButton 和 CancelButton。您可以将这些设置为引用表单上的按钮控件。AcceptButton 告诉当用户按下 Enter 键时应该单击哪个按钮,而 Cancel 按钮告诉当用户按下 Esc 键时应该单击哪个按钮。
Often you will set the DialogResult of the AcceptButton to DialogResult.OK or DialogResult.Yes and DialogResult.Cancel or DialogResult.No for the CancelButton. This ensures that you can easily check which button was clicked if you dispay the form modally.
通常,您会将 AcceptButton 的 DialogResult 设置为 DialogResult.OK 或 DialogResult.Yes,将 CancelButton 设置为 DialogResult.Cancel 或 DialogResult.No。这确保您可以在以模态方式显示表单时轻松检查单击了哪个按钮。
回答by Marc Gravell
(edit - the answer hereis very good for TextBox
; this pattern might be useful for other controls that lack the AcceptsReturn
or equivalent)
(编辑 -此处的答案非常适合TextBox
;此模式可能对缺少AcceptsReturn
或等效项的其他控件有用)
You can use the GotFocus
and LostFocus
events to change the AcceptButton
fairly easily, for example:
您可以使用GotFocus
和LostFocus
事件AcceptButton
相当容易地更改,例如:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
TextBox multi, single;
Button btn;
using(Form form = new Form {
Controls = {
(multi= new TextBox { Multiline = true, Dock = DockStyle.Fill}),
(btn = new Button { Text = "OK", Dock = DockStyle.Bottom,
DialogResult = DialogResult.OK}),
(single = new TextBox { Multiline = false, Dock = DockStyle.Top}),
}, AcceptButton = btn
})
{
multi.GotFocus += delegate { form.AcceptButton = null; };
multi.LostFocus += delegate { form.AcceptButton = btn; };
btn.Click += delegate { form.Close(); };
Application.Run(form);
}
}
回答by Dean
or you can do it in de focus event of your textbox as in
或者您可以在文本框的 de focus 事件中执行此操作,如
_targetForm.AcceptButton = _targetForm.btnAccept;
and then ubind it in the other textbox with multilines
然后用多行将它添加到另一个文本框中
回答by nijas
on Form_Load, set
在 Form_Load 上,设置
this.AcceptButton = buttonName;
回答by RAVI VAGHELA
Do as following:
执行以下操作:
private void Login_Load(object sender, EventArgs e)
{
this.AcceptButton = btnLogin;
}