Microsoft KB Archive/903898

= How to use the Windows Forms NotifyIcon component in Visual Basic .NET to display an icon for an application in the notification area =

Article ID: 903898

Article Last Modified on 11/16/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic 2005

-





This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Drawing
 * System.Windows.Forms



INTRODUCTION
This article describes how to use the Windows Forms NotifyIcon component in Microsoft Visual Basic .NET. The NotifyIcon component displays an icon for an application in the notification area. This article contains a code sample that demonstrates how to do the following:
 * Use the NotifyIcon component to display an icon for an application in the notification area in Microsoft Windows XP.
 * Create a shortcut menu.
 * Use the Click event to minimize the application in the notification area.

Important Windows XP lets you display status or notification information in the notification area. Because the notification area is a shared resource, you should only display information that is global or that must be monitored by the user when the user works with other applications. Do not automatically display an icon in the notification area just to provide convenient access to the application's features or properties. These icons add clutter to a potentially already cluttered area of the screen. You can include an option for users to display an icon in the notification area. However, always configure this option to be off by default.

Code sample
The following code sample demonstrates how to minimize an application that was developed in Visual Basic .NET and display an icon for the application in the notification area in Windows XP. To do this, follow these steps:  Start Visual Basic .NET, and then create a new Windows application. By default, Form1.vb is created.  Paste the following code into Form1.vb. Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'This call is required by the Windows Form Designer. InitializeComponent

'Add any initialization after the InitializeComponent call. NotifyIcon1.Icon = New Icon(&quot;appicon.ico&quot;)

End Sub

'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

'The following is required by the Windows Form Designer. Private components As System.ComponentModel.IContainer

'Note: The following procedure is required by the Windows Form Designer. 'It can be modified by using the Windows Form Designer. 'Do not modify it by using the code editor. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon Friend WithEvents mnuMenu As System.Windows.Forms.MenuItem Friend WithEvents mnuCreateIcon As System.Windows.Forms.MenuItem Friend WithEvents mnuExit As System.Windows.Forms.MenuItem Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents mnuRemIcon As System.Windows.Forms.MenuItem Friend WithEvents mnuAbout As System.Windows.Forms.MenuItem Friend WithEvents btnClickHere As System.Windows.Forms.Button Friend WithEvents mnuHideForm As System.Windows.Forms.MenuItem Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu Friend WithEvents cmnuExit As System.Windows.Forms.MenuItem  Private Sub InitializeComponent Me.components = New System.ComponentModel.Container Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.mnuMenu = New System.Windows.Forms.MenuItem Me.mnuAbout = New System.Windows.Forms.MenuItem Me.mnuCreateIcon = New System.Windows.Forms.MenuItem Me.mnuHideForm = New System.Windows.Forms.MenuItem Me.mnuRemIcon = New System.Windows.Forms.MenuItem Me.mnuExit = New System.Windows.Forms.MenuItem Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components) Me.ContextMenu1 = New System.Windows.Forms.ContextMenu Me.cmnuExit = New System.Windows.Forms.MenuItem Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.btnClickHere = New System.Windows.Forms.Button Me.SuspendLayout '       'MainMenu1 '       Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem {Me.mnuMenu}) '       'mnuMenu '       Me.mnuMenu.Index = 0 Me.mnuMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem {Me.mnuAbout, Me.mnuCreateIcon, Me.mnuHideForm, Me.mnuRemIcon, Me.mnuExit}) Me.mnuMenu.Text = &quot;&Menu&quot; '       'mnuAbout '       Me.mnuAbout.Index = 0 Me.mnuAbout.Text = &quot;&About the Notification Area&quot; '       'mnuCreateIcon '       Me.mnuCreateIcon.Index = 1 Me.mnuCreateIcon.Text = &quot;&Create NotifyIcon&quot; '       'mnuHideForm '       Me.mnuHideForm.Index = 2 Me.mnuHideForm.Text = &quot;&Hide Form&quot; '       'mnuRemIcon '       Me.mnuRemIcon.Index = 3 Me.mnuRemIcon.Text = &quot;&Remove Icon&quot; '       'mnuExit '       Me.mnuExit.Index = 4 Me.mnuExit.Text = &quot;&Exit&quot; '       'NotifyIcon1 '       Me.NotifyIcon1.ContextMenu = Me.ContextMenu1 Me.NotifyIcon1.Text = &quot;&quot; '       'ContextMenu1 '       Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem {Me.cmnuExit}) '       'cmnuExit '       Me.cmnuExit.Index = 0 Me.cmnuExit.Text = &quot;Exit&quot; '       'MenuItem1 '       Me.MenuItem1.Index = -1 Me.MenuItem1.Text = &quot;Shrink&quot; '       'MenuItem3 '       Me.MenuItem3.Index = -1 Me.MenuItem3.Text = &quot;Exit&quot; '       'btnClickHere '       Me.btnClickHere.Location = New System.Drawing.Point(96, 24) Me.btnClickHere.Name = &quot;btnClickHere&quot; Me.btnClickHere.TabIndex = 0 Me.btnClickHere.Text = &quot;Click Here&quot; '       'Form1 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.btnClickHere) Me.Menu = Me.MainMenu1 Me.Name = &quot;Form1&quot; Me.Text = &quot;Form1&quot; Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click Dim Caption As String = &quot;Warning&quot; MessageBox.Show(&quot;The notification area should only be used as a way to provide information to a user.&quot; _       & &quot; As a best practice, you should not use an icon in the notification area as a shortcut to start an &quot; _        & &quot;application or remove the application from the taskbar.&quot;, Caption, MessageBoxButtons.OK) mnuCreateIcon.Enabled = True mnuRemIcon.Enabled = True mnuExit.Enabled = True

End Sub

Private Sub mnuCreateIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCreateIcon.Click

Me.NotifyIcon1.Visible = True btnClickHere.Visible = True Me.NotifyIcon1.Text = &quot;Form1 is running&quot;

End Sub

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click Me.Close

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load mnuCreateIcon.Enabled = False mnuRemIcon.Enabled = False mnuExit.Enabled = False btnClickHere.Visible = False mnuHideForm.Enabled = False

End Sub

Private Sub cmnuRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Show

End Sub

Private Sub mnuRemIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRemIcon.Click Me.NotifyIcon1.Visible = False

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClickHere.Click Dim Caption As String = &quot;Notice&quot; MessageBox.Show(&quot;Check the NotifyIcon in the notification area&quot;, Caption, MessageBoxButtons.OK) Me.NotifyIcon1.Text = &quot;You clicked the button&quot; btnClickHere.Visible = False mnuHideForm.Enabled = True

End Sub

Private Sub mnuHideForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHideForm.Click Dim Caption As String = &quot;Notice&quot; MessageBox.Show(&quot;You can quit by right-clicking NotifyIcon and then by clicking Exit&quot;, Caption, MessageBoxButtons.OK) Me.Hide Me.NotifyIcon1.Text = &quot;Form1 is running in the background&quot; End Sub

Private Sub cmnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuExit.Click Me.Close End Sub End Class  To run the application, press the F5 key.

Troubleshooting
You must have an .ico file that is named Appicon.ico in the application folder.

