How to Show NotifyIcon in Windows Forms Application Using C#


Introduction

In this article I will explain how to show a notification in the status notification area of the taskbar for a Windows Forms application using the NotifyIcon component of Windows Forms.

NotifyIcon-in-Windows-Forms-application.jpg

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 taskbar. A context menu can also be added to the NotifyIcon that can be accessed by clicking the right mouse button on the icon.

Adding NotifyIcon to a Form

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

Go to the properties of the NotifyIcon and set its 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 the 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 hover (when you put the mouse over the icon in the notification area). You can write the following in the code behind:

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

Showing Balloon tooltip popup

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

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 item "Exit". (You can refer to my previous article on how to add a Context Menu.) Double-click on the "Exit" to write code for its click event. And set the ContextMenuStrip property of the NotifyIcon to the new ContextMenuStrip "contextMenuStrip1". A shortcut menu is displayed when the right mouse button is clicked on the notify icon, which contains a button ("Exit") to close the application. See:

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

Add-Context-Menu-to-NotifyIcon.jpg


Similar Articles