的WinForms验证事件prevents Esc键关闭窗体 [英] WinForms Validating event prevents Escape key closing the form
问题描述
我有一个文本框一个简单的形式,再加上确定和取消按钮。窗体的AcceptButton和CancelButton设置是否正确,以及确定和取消按钮都有自己的DialogResult设置为确认和取消。
I have a simple Form with a single TextBox, plus OK and Cancel buttons. The Form's AcceptButton and CancelButton are set correctly, and the OK and Cancel buttons have their DialogResult set to 'OK' and 'Cancel'.
我要添加验证,将prevent来自用户的文本框OK,荷兰国际集团的形式在验证失败时,而且这也将让他们取消一切正常。
I want to add validation to the TextBox which will prevent the user from OK-ing the form when validation fails, but which will also allow them to cancel as usual.
CausesValidation属性为True默认情况下,所有的控制,但我有这个改变为False上的取消按钮。
The CausesValidation property is True by default on all the controls, but I have changed this to False on the Cancel Button.
果然,单击OK(确定)或pressing回车键将运行Validating事件我连接到文本框。 pressing取消按钮绕过确证,这是完美的。
Sure enough, clicking OK or pressing the Enter key will run the Validating event I wired up to the TextBox. Pressing the Cancel button bypasses Validating, which is perfect.
不过,pressing Escape取消的形式并不会执行相同的pressing取消按钮 - 它引发Validating事件和$ P $从退出pvents用户。
However, pressing Escape to cancel the form does not perform the same as pressing the Cancel button - it raises the Validating event and prevents the user from exiting.
有什么办法使Escape键的预期效果,即不会引发Validating事件,就好像取消按钮已经pressed?
Is there any way of making the Escape key perform as intended, i.e. not raise the Validating event, just as if the Cancel button had been pressed?
一个完整的工作方案是:
A complete worked solution is:
创建一个新的Windows窗体应用程序。第二个窗体添加到项目中。
Create a new Windows Forms app. Add a second Form to the project.
粘贴此code到Form1的构造函数中,在InitializeComponent()之后:
Paste this code into Form1's constructor, after InitializeComponent():
MessageBox.Show((new Form2()).ShowDialog().ToString());
这显示了我们的第二种形式传回的的DialogResult。
This shows the DialogResult passed back from our second form.
粘贴此code到窗体2的构造,在InitializeComponent()之后:
Paste this code into Form2's constructor, after InitializeComponent():
TextBox txtName = new TextBox();
txtName.Validating +=
new CancelEventHandler((sender, e) =>
{
if (txtName.Text.Length == 3)
{
MessageBox.Show("Validation failed.");
e.Cancel = true;
}
});
Button btnOk = new Button
{
Text = "OK",
DialogResult = DialogResult.OK
};
Button btnCancel = new Button
{
Text = "Cancel",
CausesValidation = false,
DialogResult = DialogResult.Cancel
};
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Controls.AddRange(new Control[]
{
txtName, btnOk, btnCancel
});
this.AcceptButton = btnOk;
this.CancelButton = btnCancel;
this.Controls.Add(panel);
在这个简化的例子文本框不会让你继续,如果有3个字符的输入。您可以preSS取消按钮或直接即使有3个字符present关闭的形式;然而,pressing Escape键会的不可以做同样的 - 它触发Validating事件,而应该做同样为pressing取消
In this simplified example the textbox will not let you proceed if there are 3 characters input. You can press the Cancel button or close the form directly even if there are 3 characters present; however pressing the Escape key will not do the same - it fires the Validating event whereas it should be doing the same as pressing Cancel.
推荐答案
是的,ValidateChildren方法的这一尴尬的怪癖。它不知道取消之意。复制粘贴此code来解决这个问题:
Yes, this an awkward quirk of the ValidateChildren method. It doesn't know that canceling was intended. Paste this code to fix the problem:
protected override void OnFormClosing(FormClosingEventArgs e) {
base.OnFormClosing(e);
e.Cancel = false;
}
要避免一个验证事件处理程序的运行,导致的副作用,如一个消息框,这个语句添加到方法的顶部:
To avoid having a Validate event handler running that causes side-effects, like a message box, add this statement to the top of the method:
private void txtName_Validating(object sender, CancelEventArgs e)
{
if (this.DialogResult != DialogResult.None) return;
// etc..
}
粘贴此code到表单中得到的DialogResult集之前尝试验证表单:
Paste this code into your form to get the DialogResult set before it tries to validate the form:
protected override bool ProcessDialogKey(Keys keyData) {
if (keyData == Keys.Escape) this.DialogResult = DialogResult.Cancel;
return base.ProcessDialogKey(keyData);
}
这篇关于的WinForms验证事件prevents Esc键关闭窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!