Search code examples
c#performanceevent-handlingformclosing

Closing() with CancelEventArgs versus Form_Closing with FormClosingEventArgs


We are currently upgrading and remodularizing our code as we transfer from 32 to 64 bit system. In due process, one of our goals is to change from an Init() function where things were added as such example.

this.Closing += new System.ComponentModel.CancelEventHandler(this.Form_Closing);

I'd like Windows events to handle these sorts of things. So I went to the Form_Closing event in the Windows Form Events and [without surprise] saw that this was not the form_closing event. My question to this is, is there any difference between what is actually going on with CancelEventArgs vs FormClosingArgs, or are these two pieces of code literally doing the same thing with one being a Component of System and one being the result of a Windows Event handling what it does best? I'm just sort of diving and indulging myself into this new project as an intern. Is it possible to just replace the CancelEventArgs with the FormClosing one without any loss of data or issues?

Code 1: CancelArgs

 private void Form_Closing(object sender, CancelEventArgs e)
    {
        // If the user hit Cancel, just close the form.
        if (this.DialogResult == DialogResult.Ignore)
            return;

        if (this.DialogResult == DialogResult.OK)
        {
            // If the address is not dirty, just cancel out of
            // the form.
            if (!this._editedObject.IsDirty)
            {
                this.DialogResult = DialogResult.Cancel;
                return;
            }

            // Save changes.  If save fails, don't close the form.
            try
            {
                SaveChanges();
                return;
            }
            catch (Exception ex)
            {
                ShowException se = new ShowException();
                se.ShowDialog(ex, _errorObject);
                _errorObject = null;
                e.Cancel = true;
                return;
            }
        }

Form_Closing -- Preferred Route

private void ScheduleItemDetail_FormClosing(object sender, FormClosingEventArgs e)
    {
        // If the user hit Cancel, just close the form.
        if (this.DialogResult == DialogResult.Ignore)
            return;

        if (this.DialogResult == DialogResult.OK)
        {
            // If the address is not dirty, just cancel out of
            // the form.
            if (!this._editedObject.IsDirty)
            {
                this.DialogResult = DialogResult.Cancel;
                return;
            }

            // Save changes.  If save fails, don't close the form.
            try
            {
                SaveChanges();
                return;
            }
            catch (Exception ex)
            {
                ShowException se = new ShowException();
                se.ShowDialog(ex, _errorObject);
                _errorObject = null;
                e.Cancel = true;
                return;
            }
        }
    }

Solution

  • You don't get the CloseReason property with the CancelEventArgs class, which is the only difference, since FormClosingEventArgs inherits from the CancelEventArgs class. The FormClosingEventArgs was introduced in .Net 2.0.

    Alternatively, instead of using the event, you could just override the OnFormClosing method, too.

    protected override void OnFormClosing(FormClosingEventArgs e) {
      // your code
      base.OnFormClosing(e);
    }