How to add a
right click context menu to the tray icon
tutorial I 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
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.
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.
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.
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,
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,
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.
notifyIcon1_DoubleClick(object sender, EventArgs e)
3) Right Click
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.
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
toolStripMenuItem1_Click(object sender, EventArgs e)
Exit item all we need to do is call the Close() method.
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.
application and you should get something like this when you right click on the
system tray icon.
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
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.
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.
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
new Icon(GetType(), "workingIcon.ico");
//... do some checking here...
//if you have new mail
new Icon(GetType(), "newmailIcon.ico");
//else reset the icon
new Icon(GetType(), "normalIcon.ico");
It should look
something like this when it’s working.
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.