I have a WPF project in which I have a window with custom Close logic. I want some code to run when a user closes a window. I know of two ways to do this and I'm wondering which is better:
Option 1) Handle the base.Closing event.
Option 2) Override the OnClosing method.
Here's some sample code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
base.Closing += this.MainWindow_Closing;
}
//Option 1
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//close logic here, or
}
//Option 2
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
//close logic here
base.OnClosing(e);
}
}
The only difference I can find between the two options is cosmetic. I like Option 2 better because it just looks cleaner to me. I prefer overriding methods to handling events.
Are there any other differences between these two options? I know that Option 1 is provided for some other class to handle this window's Closing event.
Edit: I forgot to mention that I'm using .Net 4.0. It looks like .Net 4.5 has an OnFormClosing event that deprecates the OnClosing event. I have not used the OnFormClosing event.
The major difference is with the override you determine when the base code is called. When consuming the event, you have no control over that. When consuming the event, you are in essence, the base code.
This is an important difference at times because if there are consumers of the event but you need to do work before they get called, then you need the override.
In this case--probably not a big difference.
A class listening to its own events is a bit silly. It is however the way designers work, they were written to generate the event handler assignment instead of auto-generating the method override. Since you are not using a designer and writing the event handler assignment yourself, you should definitely favor the override. One less thing you can do wrong, you can't forget to write the assignment.
It only truly matters if you (or another programmer) derives a class from your MainWindow class. Now you have a choice on how to write your OnClosing() method:
add your custom code before the base.OnClosing() call. You allow the derived class to change the decision you made. In other words, for this event the programmer could force e.Cancel back to false. This is the normal way.
call base.OnClosing() first, then add your custom code. That puts you firmly in control with no option for the derived class to override your decision. You do this when your decision matters most and/or a derived class cannot possibly override your choice correctly, perhaps because only you have access to private info.
not call base.OnClosing(). This prevents the derived code from seeing the event at all. You'd do this when you drastically change the event handling. For this event for example when you cancel the close and, say, hide the window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With