tags:

views:

110

answers:

2

I'm building an app in C# using VS 2008 - I've added a method of checking if a file has changed when it is closed, but this only works for the File>close menu. Is there any way to get the red X in the top right to actually do anything before shutting everything? If so, how? I've only been doing C# for a few days, and this is incredibly confusing - there are no methods for the overall interface window anywhere. Help is much appreciated. Thanks.

+6  A: 

Use the Form.FormClosing event. Or the FormClosed event, that comes later and cannot cancel the closng.

And from the File|Close menuItem, just Close() the Form.

If you do that, you have 1 spot (FormClosing) where all the possible ways of closing a Form (including ALT+F4 and TaskManager) converge.

Do take a look at e.CloseReason, you don't want to be in the way when it is for example WindowsShutDown

Henk Holterman
When using these events you have to take into account the fact that on OK or Cancel buttons ( any Dialog closing Button for that matter) those events get called as well, so the code in your event handler will have to take those situations into consideration.
Anton
@Anton, correct, but from File|Closing i gathered that this was not a Dialog.
Henk Holterman
@Henk, tbh my assumption may be completely premature, I just added this to (hopefully) save the OP some work just in case :)
Anton
+3  A: 

You could probably do it through the window's closing event: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx

Greg Kurts