articles

Home / DeveloperSection / Articles / How to add a right click context menu to the tray icon

How to add a right click context menu to the tray icon

How to add a right click context menu to the tray icon

mohan kumar 42477 15-Jun-2012

in this tutorial, We will show you how to do this in a C# application using Microsoft Visual Studio. And together, I'll also show you how to add a right-click context menu to the tray icon and programmatically change the tray icon.

1) The Notify Icon Control

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

How to add a right click context menu to the tray icon

Next set the Text property on the NotifyIcon to whatever you won't appear when you hover over your icon in the system tray.
According to this case, I just put “System Tray Demo” but usually this would be the name of your application.
First, we need to set the Icon property on the NotifyIcon. We should really make a unique icon for our program to set it apart from the many other applications that often run in the system tray.
Here into 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. 

How to add a right click context menu to the tray icon

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 aswww.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 that 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.Hide();
        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.Show();
    this.WindowState = FormWindowState.Normal;
    this.ShowInTaskbar = true;
}

 

 How to add a right click context menu to the 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.

How to add a right click context menu to the tray icon

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. 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.Show();
    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)
{
    this.Close();
}

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.

 How to add a right click context menu to the tray icon

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

How to add a right click context menu to the tray icon

4) Changing the Icon

Into the few applications, A system tray icon might be changed to reflect the status of that application. For instance, An AVG Antivirus icon changes when your virus database is out of date.
In this instance, imagine you have an email checking application. You have already set the normal icon, but what if we have new mail? You might want a different icon to show that we have a new mail.
To do this right-click on your project in the Visual Studio Solution Explorer. Then go to “Add -> Existing Item…”. And If you want to add any type of icon. So here you can add it easily.
And in this instance, I am adding three icons: the normal icon.ico, Workington.ico and newmailIcon.ico,etc. If you want to include another icon then you should see your icon in Solution Finder.

How to add a right click context menu to the tray icon

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 the 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.

How to add a right click context menu to the tray icon

Conclusion

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.

 

You should also read this Article - Print and Print Preview separately using HTML, CSS and JavaScript


c# c# 
Updated 24-Feb-2020
Having around 5 Years experience in .NET domain.

Leave Comment

Comments

Liked By