I am trying to determine if my application is closed through clicking the "X" on the windows form, or if they clicked an "Exit" button I have on it. Right now I am using StackTrace.GetFrame(someIndex) to determine how, but i am looking for a more definitive way since it looks like these frame orders arent guaranteed. Is there a better way to make the distinction? This is a .NET 3.5 WinForm, and Im writing in C#.
+4
A:
Use a different event to handle your own "Exit" button click. In your own "Exit" event handler do your extra logic, or set some state variable, and then call the normal application close method.
Most some samples of how you have your events wired up and I get give a more detailed example of whe I mean.
It would look something like this:
private void btnMyExit_Click(object sender, EventArgs e)
{
// TODO: add any special logic you want to execute when they click your own "Exit" button
doCustomExitWork();
}
public static void OnAppExit(object sender, EventArgs e)
{
doCustomExitWork();
}
private void doCustomExitWork()
{
// TODO: add any logic you want to always do when exiting the app, omit this whole method if you don't need it
}
auujay
2010-03-19 19:27:25
ya, different events is what i was thinking, but this is code left by a more senior developer and I was just taking his code as "the right way"
Mike_G
2010-03-19 19:32:08
A:
Use the FormClosing event and query the FormClosingEventArgs for the enum CloseReason value.
Beaner
2010-03-19 19:31:40
unfortunately clicking the X in a forms corner, and clicking a button that exits the app both have the same enumeration
Mike_G
2010-03-19 19:38:51
@Mike_G - didn't read your question closely enough. Now I am curious about what difference it makes?
Beaner
2010-03-19 20:07:06
@Beaner: You mean in regards to why we want to know how they are closing it? We are trying to emulate a "minimize on X click" like that of MSN messenger.
Mike_G
2010-03-19 20:25:03