Add a Notify Icon to the System Tray with C#

Windows Forms

Add a Notify Icon to the System TrayBack in the late 90’s before the invention of the .NET Framework, there was no “easy” way to add an icon to the system tray of Windows. Developers who preferred to use Microsoft technologies were either developing with C++, Visual Basic, or maybe even FoxPro, and none of these technologies provided an easy way to integrate a notify icon into your application. This was a shame because back then the notify icon was considered as a cool feature to have for your application – every developer wanted one.

Then with the invention of the .NET Framework came the NotifyIcon .NET Component. Finally there was an easy way to add a tray icon to your application.

In this article I am going to show you how to use the NotifyIcon component.

To start off create a C# Windows Forms Application project and on your main form drag the NotifyIcon component from the toolbox to the form. From the component’s properties you can set the icon to be displayed in the system tray, the text to be displayed when the mouse hovers over the tray icon, balloon tip settings, and other common properties that most of the .NET components have.

Now for this example add a button to your form and add the following code to its click event handler:

this.WindowState = FormWindowState.Minimized;

notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon.BalloonTipTitle = "Notify Icon Test Application";
notifyIcon.BalloonTipText = "You have just minimized the application." + 
                            Environment.NewLine + 
                            "Right-click on the icon for more options.";

notifyIcon.ShowBalloonTip(5000);

The above code is minimizing the form and then setting the balloon tip properties for the NotifyIcon we added earlier. Finally we are calling the ShowBalloonTip method which as a parameter accepts the number of milliseconds you want to display the balloon for – in this case 5 seconds.

Now at this point you should be able to see something like this (the icon will vary according to what you selected from the NotifyIcon‘s properties):

Notify Icon

At the moment when you right-click on the tray icon nothing happens. This is because we have to add a ContextMenuStrip component to our form and assign it to the NotifyIcon. Add the control to your form and from the ContextMenuStrip property of the NotifyIcon select the name of your context menu.

And there you have it – the menu will pop up when you right click the tray icon. But the context menu is still empty so first you must add a few items to it. Add a menu item to restore the application and another to exit. Then your code should be something like this:

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    this.Close();
}

private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Normal;
}

As you can see the NotifyIcon is a very simple component to use. You can fully control its behaviour through code so you can display warning bubbles or error bubbles to the user very easily, plus you can attach whatever functionality you like to the context menu which can make your application easier to use.

I hope you found this article useful. Feel free to leave any comments.

Dave

6 comments… add one
  • Tanu shree Link Reply

    Found very helpful….nice post Dave

  • payman Link Reply

    thanks a lot dude i love it

  • Amol Link Reply

    Simple but Best article.
    Really good and Helpful.

  • AMG Link Reply

    Thanksssssssssssssssssssssssss

  • Prasanna86k Link Reply

    Nice and clear. Thank u.

  • A. DRAME Link Reply

    Thank you Bro, very useful !

Leave a Comment