When creating desktop applications it is sometimes necessary for the application to run in the background, for example firewall, anti-virus and media player software. Instead of permanently taking up space in your taskbar these applications are accessible from the system tray.

This tutorial will show you how to do this in a C# application using Microsoft Visual Studio. I will also show you how to add a right click context menu to the tray icon and how to programmatically change the tray icon.

1) The Notify Icon Control

The control used to make an icon appear in the system tray is a NotifyIcon control. So go ahead and make a new project. Open up the Visual Studio Toolbox and drag a NotifyIcon onto your form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.

NotifyIcon Control

Next set the Text property on the NotifyIcon to whatever you want appear when you hover over your icon in the system tray. In this case I just put “System Tray Demo” but usually this would be the name of your application.

Next you need to set the Icon property on the NotifyIcon. You should really make a unique icon for your program to set it apart from the many other applications that often run in the system tray. In this example I’m going to use an email icon (you’ll see why later when we change the icons). So go ahead and choose it.

NotifyIcon Properties

Note: When I say choose an icon file i don’t just mean any image format. I mean a .ico file. Many image editing applications allow you to save in this format. If you don’t have such an application you can use a website such as www.favicon.cc to generate one.

2) Events

Now time for some coding. In this example our program will minimize to the tray but you can do the same thing for other events such as the window closing etc.

In MS Visual Studio click on your form (in design mode). Then in the properties box click on the small lightning icon (Events) near the top of the properties box. Scroll down to the “Layout” sub-heading and double click on the Resize event. This will generate some code which should look a bit like this.

private void Form1_Resize(object sender, EventArgs e)

Next add the following code so that when the window is minimized it will be hidden and not shown in the taskbar.

private void Form1_Resize(object sender, EventArgs e)
    if (WindowState == FormWindowState.Minimized)
        this.ShowInTaskbar = false;

Now before you get too excited and try and run the program remember we need to be able to show the window again. To do this click on notifyIcon1 (in design mode) then go to its events list (same as above) and double click on the DoubleClick event. Then add the following code so the window will be restored when the system tray icon is double clicked.

private void notifyIcon1_DoubleClick(object sender, EventArgs e)
    this.WindowState = FormWindowState.Normal;
    this.ShowInTaskbar = true;

Now you can try out your program. Run it and make sure it is hidden when you click the minimize button and it can be restored when you double click the system tray icon.

System Tray Icon

3) Right Click Context Menu

To make it easy to access your application from the system tray the next thing we will add is a right click context menu. To do this go to the Visual Studio Toolbox and drag a ContextMenuStrip control onto your form. It will be placed below the form again as it has no visual representation on the form itself.

Right click on the contextMenuStrip1 and click “Edit Items…”. Add two Menu Items and change their Text properties to “Show” and “Exit”. Obviously in your own application you can add whatever you want here.

Context Menu Items

When you click on contextMenuStrip1 (in design mode) you should see the menu appear on your form. Don’t worry it won’t actually be there when you run your application. As before double click on the items to create an event for them. For the Show item we just need to copy the code from the notifyIcon1_DoubleClick method we made above. You should have something like this.

private void toolStripMenuItem1_Click(object sender, EventArgs e)
    this.WindowState = FormWindowState.Normal;
    this.ShowInTaskbar = true;

For the Exit item all we need to do is call the Close() method.

private void toolStripMenuItem2_Click(object sender, EventArgs e)

The last step is to set the NotifyIcon control’s ContextMenuStrip property to our new context menu. Do this the same way you set other properties. The name of our context menu should appear in the dropdown box.

Notify Icons Context Menu Strip

Run your application and you should get something like this when you right click on the system tray icon.

Right Click Context Menu

4) Changing the Icon

In some applications the system tray icon might change to reflect the status of the application. For example the AVG Antivirus icon changes when your virus database is out of date. In this example imagine we have an email checking application. We have already set the normal icon, but what if we have new mail? We might want a different icon to show that we have new mail.

To do this right click on your Project in the Visual Studio Solution Explorer. Then go to “Add -> Existing Item…”. Add any icons you want to add. In this example I am adding three icons: normalIcon.ico, workingIcon.ico and newmailIcon.ico. You should see your icons in the solution explorer.

Solution Explorer

Next you must set the Build Action properties of all your icons to Embedded Resource. If you don’t do this it won’t work. Setting properties is just like above.

Now changing your icons is actually quite easy. All you have to do is programmatically set your icons. For example.

public void checkMail()
    //Set a working icon
    this.notifyIcon1.Icon = new Icon(GetType(), "workingIcon.ico");
    //... do some checking here...
    //if you have new mail
    this.notifyIcon1.Icon = new Icon(GetType(), "newmailIcon.ico");
    //else reset the icon
    this.notifyIcon1.Icon = new Icon(GetType(), "normalIcon.ico");

It should look something like this when it’s working.

Changing Icons


In this tutorial we have looked at putting your C# program in the system tray, adding a context menu to the tray icon and programmatically changing the tray icon. Any comments and suggestions you have are always appreciated.

Download the Source Code

Related Posts

  • No Related Posts


If you enjoyed this post, please consider promoting it.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Design Float
  • Reddit
  • Mixx
  • Technorati

Remeber to subscribe to the feed and get future articles delivered to your feed reader.

If you want to discuss this tutorial or any other thoughts you have then you can do so over on our fourm.


  1. Comment by Sathish on August 12, 2008 1:32 pm

    This is one of the good article i have read simple, short and to the point description with an example. Not all the article are written this way….

  2. Comment by notime on October 21, 2008 8:37 pm

    Thx you very much

    about my project “System tray”

  3. Comment by Johan on November 1, 2008 8:51 pm

    The best article I have seen on the free sites out there on this subject. Made programming the control easy and fun. Keep up the great work.

  4. There is nothing more redundant which uses more of your time, from one project to the next, than coding how your project is displayed at startup, what is shown during execution, NotifyIcon? Taskbar? When is a Taskbar displayed? During normal Display? When you are minimized? Your Context Menu logic, your exit logic, your deployment logic and your update logic.

    Click here for more detail

Leave a Comment