protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e); // here?
if (cancelCondition)
{
base.OnClosing(e); // or here?
e.Cancel = true;
base.OnClosing(e); // or here?
}
base.OnClosing(e); // or here?
}
I've tried a few different places, and it seems to work anywhere, just wondering if it matters. Does base.OnClosing(e);
actually do anything?
From MSDN :
A type that derives from Window may override OnClosing. The overridden method must call OnClosing on the base class if Closing needs to be raised
In your case there seems to be no need to actually raise the Closing
event, hence it doesn't matter if and where you call base.OnClosing
.
You may however avoid to decide this in the first place if you do not override the OnClosing
method, but simply add a Closing
handler instead:
<Window ... Closing="Window_Closing">
...
</Window>
private void Window_Closing(object sender, CancelEventArgs e)
{
if (cancelCondition)
{
e.Cancel = true;
}
}
You should be overriding OnClosing
if you want to change how the closing event is fired.
If you want to execute code when the form is closing and have it run before any other Closing
handlers, put your other code before base.OnClosing
.
If you have code that you want to run after all other handlers have run, put that code after a call to base.OnClosing
.
If you want to only conditionally run all of the other handlers, put base.OnClosing
inside of some sort of conditional block so that it is only called when you want the other handlers to be run.
There is no one right answer for where it belongs in all cases. You should put it wherever you want "all of the other event handlers" to be run, relative to whatever code your adding.
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.