How to show NotifyIcon in Windows Forms application using C#

In this article I will explain how to show NotifyIcon in Windows Forms application using C#

Introduction

In this article I will explain how to show notification in Windows Forms application using NotifyIcon component of Windows Forms

NotifyIcon

NotifyIcon is a component in Windows Forms that is used to notify users by displaying an icon and an optional popup Balloon tooltip in the notification area of the system. A context menu can also be added to the NotifyIcon that can be accessed by clicking right mouse button on the icon

Adding NotifyIcon to a Windows Form

Create a new Windows Forms application and drag a NotifyIcon component to the Form1 from the Toolbox

Go to the property of NotifyIcon and set its using Icon property. Click on the ellipsis (…) next to the Icon property and select an icon (.ico) file or click on the “Choose icon” link at bottom of the property window. To show a NotifyIcon when a Form is open you just need to set its Icon property. To show multiple NotifyIcons in the notification area you only need to set their Icon properties. You can set its Text property that is displayed on mouse over (when you put mouse over the icon in the notification area). You can write following in the code behind do the same

notifyIcon1.Icon = new System.Drawing.Icon(@"D:Datablogger.ico");
notifyIcon1.Text = "My applicaiton";
notifyIcon1.Visible = true;

Showing Balloon tooltip popup

To show a balloon tooltip popup you need to set BalloonTipIcon property to one of the BalloonTipIcon ennumerations, ToolTipIcon.Info, ToolTipIcon.Error or ToolTipIcon.Warning. By default it is set to ToolTipIcon.None. An information, error or a warning icon is displayed on the balloon tooltip according to this property. If you set BalloonTipIcon property to one of these, you must set some text in BalloonTipText property otherwise you will get an exception message “Balloon tip text must have a non-empty value”. BalloonTipText property is used to set tooltip text of the balloon. You can set title of the balloon tooltip using BalloonTipTitle property. Finally, ShowBalloonTip() method is used to show balloon tooltip. It takes a timeout parameter that is an integer value which specifies for how many milliseconds balloon should display. Following code will display balloon tooltip when the form loads and stays for 1000 milliseconds

private void Form1_Load(object sender, EventArgs e)
{
     notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
     notifyIcon1.BalloonTipText = "I am a NotifyIcon Balloon";
     notifyIcon1.BalloonTipTitle = "Welcome Message";

     notifyIcon1.ShowBalloonTip(1000);
}

Add Context Menu to NotifyIcon

To add a context menu to the NotifyIcon, drag a ContextMenuStrip on the Form and add a menu “Exit” (You can refer to my previous article on how to add a Context Menu). Double click on the “Exit” to write code in its click event. And set ContextMenuStrip property of the NotifyIcon to the new ContextMenuStrip “contextMenuStrip1”. A shortcut menu is displayed on right mouse click on the notify icon, which contains a button (“Exit”) to close the application

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
     Application.Exit();
}

2 thoughts on “How to show NotifyIcon in Windows Forms application using C#

Leave a Reply

Your email address will not be published. Required fields are marked *