3

I mean exit to system tray, not minimize to system tray, ie when you click on the "red cross" in the top right corner on a Windows form, instead of application closing, it runs in the system tray instead.

Aperture
  • 2,387
  • 4
  • 25
  • 47
  • I see you ask 2 ways, one you say you want it to exit, as in the process is no longer running. the other, you want it running in the system tray. Depending on which you actually want (You can't have both), my answer or ajay_whiz/Dave's answers maybe be correct – Michael Baldry Aug 25 '10 at 08:11

3 Answers3

3

You can hook to Form Closing event and cancel the closing event and hide the form

sample code

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
ajay_whiz
  • 17,573
  • 4
  • 36
  • 44
2

Handle the FormClosing event, block the Close (e.Cancel = true) and instead minimize to the system tray as you would if you were, well, minimizing to the system tray.

You will, of course, have to have a condition under which closing the form doesn't minimize to the system tray. For example, tracking the state and if the form is already minimized then allowing the form to actually close.

Community
  • 1
  • 1
Dave D
  • 8,472
  • 4
  • 33
  • 45
1

It is not possible to 'exit' to the system tray, a process with a message queue must be running to allow an icon to appear in the tray and function correctly (e.g. not disappear when you hover over it)

The way adobe and others handle this is to have a separate application that just does the system tray icon, when you click it and your application isn't running it starts it, if the application is already running, it brings it to the foreground.

Michael Baldry
  • 1,990
  • 2
  • 14
  • 28