Hi!
I'm working on an application that only sits in the notification area and keeps track of time. At certain intervals, it will notify the user. It also has a form, with which the user can configure and see the time etc.
When the form is closed with the little red x in the upper right corner of the form, I just want the form to hide but still run in the background. The application should only exit when the user selects the "exit" context menu item of the notification icon.
This is how i have managed to solve this:
//This is called when the context menu item "exit" is selected
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
//This is always called then the form is about to close, regardless of
//what or who innitiated the close event (as far as I understand)
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
Hide();
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
You can see that the way I did was to call Application.Exit() in the context menu method. I only did this in order to be able to distinguish between calls from the menu item and other calls to the close method.
It works fine, but I don't like it because in both cases it is the user that sets off the event so in both cases, the CloseReason should be UserClosing.
My question is therefore: is there a more appropriate way of doing this?
/Robert