【发布时间】:2011-02-17 22:12:37
【问题描述】:
我计划重写 OnFormClosing(System.Windows.Forms.Form)来验证用户对我的对话框的输入。如果验证失败,那么我将 FormClosingEventArgs 的 Cancel 属性设置为 true:
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (DialogResult == DialogResult.OK)
{
if (!IsDialogInputValid())
{
e.Cancel = true;
return; // Is not calling the base class OnFormClosing okay here?
}
}
base.OnFormClosing(e);
}
我的问题:即使取消关闭,我是否应该调用基类的 OnFormClosing(也就是说,我是否应该删除上面的提前返回)?
我目前的想法是我不应该调用它,因为当对话框本身决定它没有关闭时,附加到 FormClosing 事件的委托不会被调用。另一方面,我很担心基类的 OnFormClosing 可能会做其他必要的事情。
仅供参考,我是 Winforms 的新手,因此对于我应该如何执行验证(如果这不是最好的方法)的任何建议,我们将不胜感激。
相关链接: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx
【问题讨论】: