How to Create a System tray Notification in C#

By FoxLearn 7/19/2024 2:24:10 AM   8.51K
To create a system tray application in C# using the NotifyIcon control, how to minimize your application to the system tray, display a balloon tip with custom title and description, and ensure proper handling of application exit.

How to create a System tray Notification in C#

Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SystemTray" and then click OK

Design your form as below

c# system tray

Drag and drop a NotifyIcon, ContextMenuStrip controls from the toolbox onto your form. You can find it in the "Common Controls" section of the toolbox in Visual Studio.

c# notifyicon

Select the NotifyIcon on your form and set its properties in the Properties window choose an icon that represents your application when it is minimized to the system tray. This can be set by clicking the ellipsis (...) next to the Icon property and selecting an icon file, then set the Text property to a tooltip text that will be displayed when the user hovers over the system tray icon.

You can attach a context menu to the NotifyIcon to provide options when the user right-clicks the icon by populating the ContextMenuStrip with items that you want to show in the menu (e.g., "Open", "Exit"), then set the ContextMenuStrip property of the NotifyIcon to the ContextMenuStrip control you just created.

To show or hide the form from the system tray icon, you can use the NotifyIcon's MouseDoubleClick event.

// windows notification c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Exit your program
            Application.Exit();
        }

        private void Form1_Move(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                //Show ballon tip
                notifyIcon1.ShowBalloonTip(1000, "Important notice", "Something important has come up. Click this to know more.", ToolTipIcon.Info);
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }
    }
}

VIDEO TUTORIAL