Minimize to System Tray - C# and Windows Forms

Overview

This article explains how to have a Windows Form app minimize to the System Tray when the close button is clicked.

Note: You'll need a NotifyIcon on the form of interest, along with a context menu to quit the application.

Code

// When the user attempts to close the form, this code hides the form instead of closing it
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
        Hide();
    }
}
// Clicking the NotifyIcon will restore the form to its normal state
private void uxNotifyIcon_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Show();
        WindowState = FormWindowState.Normal;
        Activate();
    }
}
// This is the code behind the "Quit" item on the NotifyIcon's context menu
private void uxQuitMenuItem_Click(object sender, EventArgs e)
{
    Application.Exit();
}