omicronlyrae
omicronlyrae

Reputation: 255

Using close button in C#

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.

Upvotes: 2

Views: 986

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273691

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

Upvotes: 6

Gregory Kurts
Gregory Kurts

Reputation: 83

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

Upvotes: 3

Related Questions