views:

281

answers:

4

I want my application such that, it will minimize to System Tray on clicking the close(X) button.

And it will only be closed by clicking a different button/menu on the main application window or clicking a system tray context menuItem.

I am able to make the window minimize to tray on close.

But the problem I am facing is, I am now unable to close the application.

This is my code (it is unable to close the application):

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void hideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = true;
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.DoEvents();
            Application.Exit();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                notifyIcon1.ShowBalloonTip(500);
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.WindowState = FormWindowState.Minimized;
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }        
    }
+8  A: 

In the button, set a field, for example:

bool isClosing;
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
    isClosing = true;
    Close();
}

and check this in the "closing":

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if(!isClosing) {
        e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;
    }
}
Marc Gravell
+2  A: 

That's because it always hits the form closing event handler where you cancel the event.

Place a condition there and skip canceling the event if the form is already minimized

mfeingold
+2  A: 

I might recommend a slightly different approach, the main issue that you have is that your application is running as the form, so you have a lot of workarounds to ensure that the form doesn't close.

What I do, when working with an application that really lives in the system tray is to create a custom application context, which actually simplifies the process. Here is an article that I wrote that shows you how to do it.

Mitchel Sellers
+2  A: 

Add a flag in places where you are overriding close behavior, and also cover other exit cases by checking the CloseReason enumeration value on the close event argument.

bool m_NeedClose = false;

private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
    m_NeedClose = true;
    Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if(m_NeedClose ||
      (e.CloseReason != CloseReason.UserClosing))
    {
        return;
    }

    e.Cancel = true;
    this.WindowState = FormWindowState.Minimized;
}

FormClosingEventArgs @ MSDN
CloseReason Enumeration @ MSDN

meklarian