Override the OnFormClosing form for validation

I plan to override OnFormClosing (from System.Windows.Forms.Form) to perform a user input check in my dialog. If the validation fails, then I set the FormClosingEventArgs Cancel property to 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);
}

My question is: Do I have to refer to the OnFormClosing base class even when I cancel the closure (that is, should I remove the earlier return above)?

My current thinking is that I should not call it, because delegates attached to the FormClosing event will not be called when the dialog itself decides that it does not close. On the other hand, I'm nervous that perhaps the OnFormClosing base class does other necessary things.

FYI, Winforms, , ( ), .

: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx

+5
4

. , , base.OnFormClosing(e) ( ), , .

, OnFormClosing? / , DialogResult ""? , OK, Close() . OnFormClosing , ( , X ).

, , . base.OnFormClosing(), . base.OnFormClosing() , , .

+6

base.OnFormClosing() , FormClosing . , e.Cancel true, . FormClosing false? , . , e.Cancel, ? .

, .

+4

FormClosing, OnFormClosing, FormClosing e.Cancel true , ...

-1

. :

public Form1()
    {
        InitializeComponent();
        this.FormClosing += ThisFormClosing;
    }

    void ThisFormClosing(object sender, FormClosingEventArgs e)
    {
        if (YouDontWantToClose)
        {
            e.Cancel = true;
            return;
        }

        //Do Some Extra Work Here
    }
-1

All Articles