简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM