tags:

views:

1284

answers:

2

Is there an event that is fired when the user presses the close button?

This is because the Window Closing event is fired both when one closes the window manually (with the Close method) and also when the user presses the [X] button...but I somehow need to know only when the user presses the [X] button not when the window is closed manually.

+2  A: 

I don't believe there is a way to tell those apart in WPF (though I'm not positive).

The way I always handled it in WinForms was to create a member variable "_Closing", set it to false, and a method "ReallyClose()" that would set _Closing to true, then call Close. My Closing handler would then cancel the close if _Closing was not set to true.

Yeah, it's a bit of a hack, but it worked.

Jonathan
A: 

I also don't think there is a way to tell them apart. You can put a handler on the Application.Exit event, but it doesn't distinguish between "red X button close" and "alt-F4 close" (or whatever other types of close you are considering).

BTW, if you check for Application.Exit, be sure to check for Application.SessionEnding too - if someone logs off while your app is running, you can't be guaranteed that Application.Exit will be called.

Bruce