Windows Forms 2.0 – The NotifyIcon Control Recipes

Posted by: Suprotim Agarwal , on 3/21/2007, in Category WinForms & WinRT
Views: 17960
Abstract: In this article, we will explore how to perform some common tasks using the NotifyIcon Control of Windows Forms 2.0.
Windows Forms 2.0 –NotifyIcon Control Recipes
A NotifyIcon displays an icon in the system tray or notification area, for processes that run in the background. It is essentially suitable for programs providing virus protection or for volume control. NotifyIcons indicate the application’s status without requiring the user’s attention.
How Do I Get Started With The NotifyIcon Component 
The following code example demonstrates how to use the NotifyIcon component to display an icon in the notification area. When the user runs the application, a NotifyIcon as displayed in Figure ---,gets displayed in the notification area. When the DoubleClick event occurs, the application form is activated by calling the Form.Activate method. In order to close the form, a context menu can be added to the NotifyIcon which will be demonstrated in later examples.
private void CreateNotifyIcon()
this.container = new Container();
nIcon = new NotifyIcon(this.container);
// Displays a TV Icon
nIcon.Icon = new Icon(@"Resources\App.ico");
nIcon.Text = "NotifyIcon Example. DoubleClick to restore the form";
nIcon.Visible = true;
nIcon.DoubleClick += new EventHandler(nIcon_DoubleClick);
this.WindowState = FormWindowState.Minimized;
// Add this line to hide the form in the taskbar
this.ShowInTaskbar = false;
private void nIcon_DoubleClick(object sender, EventArgs e)
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;
// Activate the form
Note: To Show a Balloon Tip as shown in Figure ---, all you need to do is use the ShowBalloonTip property as demonstrated below:
private void nIcon_DoubleClick(object sender, EventArgs e)
private void dispBalloonTip()
nIcon.ShowBalloonTip(20, "Information", "This is a balloon tool tip",
How Do I Add A ContextMenu To The NotifyIcon
It is common to associate a ContextMenu with a NotifyIcon. This enables you to right-click this icon and execute commands.
The following code example extends the earlier application, by associating a ContextMenu with the NotifyIcon component. To run the application, add the AddContextMenu() to the constructor as shown below and create an instance of the ContextMenu and MenuItem class. Finally set the ContextMenu property of the NotifyIcon component to the contextMenu you just created.
public frmNotifyIcon()
. . .
private void AddContextMenu()
ContextMenu contextMenu1 = new ContextMenu();
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
new System.Windows.Forms.MenuItem[] {menuItem1,menuItem2});
menuItem1.Index = 0;
menuItem1.Text = "&Restore";
menuItem1.Click += new System.EventHandler(menuItem1_Click);
menuItem2.Index = 1;
menuItem2.Text = "E&xit";
menuItem2.Click += new System.EventHandler(menuItem2_Click);
nIcon.ContextMenu = contextMenu1;
private void menuItem1_Click(object Sender, EventArgs e)
// Restore the Application
this.WindowState = FormWindowState.Normal;
private void menuItem2_Click(object Sender, EventArgs e)
// Close the application.
nIcon.Visible = false;
How Do I Access The Notify Icon In A Different Form
It is possible to access the NotifyIcon from a different form altogether. For this we will create a new form called frmNew and add a button “Access NotifyIcon” to it. On the click of this button, we will access the BalloonTip created in frmNotifyIcon.
To do this, you can use this code:
// Calls the Form frmNew
private void btnNewFrm_Click(object sender, EventArgs e)
frmNew frm = new frmNew();
In the Form frmNew, write this code in the btnAccess_Click event.
private void btnAccess_Click(object sender, EventArgs e)
frmNotifyIcon frmNotify = (frmNotifyIcon)Application.OpenForms["frmNotifyIcon"];
Conclusion :
In this article, we saw a recipe approach to performing some of the most common tasks with the NotifyIcon Control. I hope the article was useful and I thank you for viewing it.
What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Amit Jaiswal on Tuesday, December 4, 2007 4:05 AM
The Article is good but i am not able to get the full knowledge of notification icon.......
Comment posted by shyam veer tyagi on Monday, June 22, 2009 3:42 AM
Nice , keep it up





Free DNC .NET Magazine



jQuery CookBook