C# application closing problem

1.1k views Asked by At

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;
        }        
    }
4

There are 4 answers

0
Marc Gravell On BEST ANSWER

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;
    }
}
0
mfeingold On

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

0
Mitchel Sellers On

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.

0
meklarian On

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