开发者

c# WinForms form still closes after setting e.Cancel = true in Form_FormClosing event

This is the code in question:

    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;
        }
    }

I've added breakpoints to the e.Cancel = true; line to ensure it's being executed.

The form closes immediately after clicking Ok.

Here's the code that calls FormAccounting:

    private void buttonAccounts_Click(object sender, EventArgs e)
    {
        FormAccounting NewFormAccounting = new FormAccounting();
        NewFormAccounting.Show();
    }


Canceling the form close event works to prevent:

  1. User closing the form
  2. Application.Exit from exiting the application
  3. Code from calling Form.Close on the form

But it does not work to prevent:

  1. User closing application's main form
  2. Code calling Form.Dispose on the form
  3. Code calling Form.Close on the application's main window

The last 3 cases don't even trigger the form close event on the non-main form, so the form goes away without a chance to cancel it. Perhaps your application is causing the form to first close in one of the first 3 ways, which triggers the event, and then in one of the second 3 ways (or something similar), which does not trigger the event and forces the form closed anyway.

Edit: Add this function to your form's code and it will allow you to review in the debugger what the call stack looks like when your window is getting closed so you can see what is actually causing it:

  protected override void DestroyHandle()
  {
     System.Diagnostics.Debugger.Break();
     base.DestroyHandle();
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜