How to use Notify icon in VB.NET

In this article we will discuss about notify icon control in, basically notify control is used for showing process that run in the background and don’t have their own interface. In Windows Services, we can use these notify icon’s to associate various functionality.we can also use this icon in window application for launching another application or other functionality. Notify icon shows in system tray.

For better understanding we can make an application as utility for showing time (clock) that also shows in system tray. The following code example demonstrates using the Notify Icon class to display an icon for an application in the system tray. The example demonstrates setting the Icon, Context Menu, Text, and Visible properties and handling the DoubleClick event

Download Source Code

Add new form Form1 and add notify icon, context menu and timer control.

Add two contextmenu item in contextMenuStrip1

1.Open(for showing application)

2.Exit (for closing application)

Now there is need to set some properties of notify icon like :

ContextMenu: Gets/Sets Context menu for the tray icon
Icon: Gets/Sets current icon
Text: Gets/Sets tooltip text that is displayed when the mouse hovers over the system tray
Visible: Gets/Sets if the icon is visible in the windows system tray

On form closing event put this code, this code minimize the form

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  If Me.WindowState = FormWindowState.Normal Then
     e.Cancel = True
     Me.WindowState = FormWindowState.Minimized
   End If
End Sub

And for showing clock form again we can use MouseDoubleClick event  of notify icon control like this:

Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
   Me.WindowState = FormWindowState.Normal
End Sub

And it will show time on labels

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
   Label1.Text = DateTime.Now.ToLongTimeString()
   Label2.Text = DateTime.Now.ToLongDateString()
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Timer1.Enabled = True
   Label1.Text = DateTime.Now.ToShortTimeString()
   Label2.Text = DateTime.Now.ToLongDateString()
End Sub

We can show tooltip on icon like Figure2 with the help of this code:

Private Sub NotifyIcon1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
    NotifyIcon1.Text = DateTime.Now.ToLongTimeString
End Sub

Finally application will look like :

Note:   1.Notify Icon class is sealed class so it cannot be inherited.

  1. The Visible property must be set to true for showing icon
  2. You  can add balloon tooltip on icon with the help of these properties :

notifyIcon1.BalloonTipTitle = “BalloonTipTitle”
notifyIcon1.BalloonTipText = “BalloonTipText.”