fen*_*nix 5 c# winforms formclosing
这是有问题的代码:
private void FormAccounting_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.FormAccountingLocation = this.Location;
Properties.Settings.Default.Save();
if (IsEditing)
{
MessageBox.Show("Please save or cancel open transactions before closing the accounting window.", "Open Transactions", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
}
}
Run Code Online (Sandbox Code Playgroud)
我在线上添加了断点以e.Cancel = true;
确保它被执行.
单击"确定"后,表单立即关闭.
这是调用FormAccounting的代码:
private void buttonAccounts_Click(object sender, EventArgs e)
{
FormAccounting NewFormAccounting = new FormAccounting();
NewFormAccounting.Show();
}
Run Code Online (Sandbox Code Playgroud)
Blu*_*kMN 13
取消表单关闭事件可以防止:
但它无法阻止:
最后3个案例甚至没有触发非主表单上的表单关闭事件,因此表单没有机会取消它.也许你的应用程序导致表单首先以前三种方式之一关闭,触发事件,然后以第二种方式之一(或类似方式),这不会触发事件并强制关闭表单.
编辑: 将此函数添加到表单的代码中,它将允许您在调试器中查看窗口关闭时调用堆栈的外观,以便您可以看到实际导致它的原因:
protected override void DestroyHandle()
{
System.Diagnostics.Debugger.Break();
base.DestroyHandle();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5267 次 |
最近记录: |