How to use the Windows Forms NotifyIcon component in Visual Basic .NET to display an icon for an application in the notification area (903898)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
- 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
#Region " Windows Form Designer generated code "
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("appicon.ico")
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
<System.Diagnostics.DebuggerStepThrough()> 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 = "&Menu"
'
'mnuAbout
'
Me.mnuAbout.Index = 0
Me.mnuAbout.Text = "&About the Notification Area"
'
'mnuCreateIcon
'
Me.mnuCreateIcon.Index = 1
Me.mnuCreateIcon.Text = "&Create NotifyIcon"
'
'mnuHideForm
'
Me.mnuHideForm.Index = 2
Me.mnuHideForm.Text = "&Hide Form"
'
'mnuRemIcon
'
Me.mnuRemIcon.Index = 3
Me.mnuRemIcon.Text = "&Remove Icon"
'
'mnuExit
'
Me.mnuExit.Index = 4
Me.mnuExit.Text = "&Exit"
'
'NotifyIcon1
'
Me.NotifyIcon1.ContextMenu = Me.ContextMenu1
Me.NotifyIcon1.Text = ""
'
'ContextMenu1
'
Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.cmnuExit})
'
'cmnuExit
'
Me.cmnuExit.Index = 0
Me.cmnuExit.Text = "Exit"
'
'MenuItem1
'
Me.MenuItem1.Index = -1
Me.MenuItem1.Text = "Shrink"
'
'MenuItem3
'
Me.MenuItem3.Index = -1
Me.MenuItem3.Text = "Exit"
'
'btnClickHere
'
Me.btnClickHere.Location = New System.Drawing.Point(96, 24)
Me.btnClickHere.Name = "btnClickHere"
Me.btnClickHere.TabIndex = 0
Me.btnClickHere.Text = "Click Here"
'
'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 = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
Dim Caption As String = "Warning"
MessageBox.Show("The notification area should only be used as a way to provide information to a user." _
& " As a best practice, you should not use an icon in the notification area as a shortcut to start an " _
& "application or remove the application from the taskbar.", 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 = "Form1 is running"
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 = "Notice"
MessageBox.Show("Check the NotifyIcon in the notification area", Caption, MessageBoxButtons.OK)
Me.NotifyIcon1.Text = "You clicked the button"
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 = "Notice"
MessageBox.Show("You can quit by right-clicking NotifyIcon and then by clicking Exit", Caption, MessageBoxButtons.OK)
Me.Hide()
Me.NotifyIcon1.Text = "Form1 is running in the background"
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.
TroubleshootingYou must have an .ico file that is named Appicon.ico in the
application folder. REFERENCESFor more information about how to use the NotifyIcon
component, visit the following Microsoft Developer Network (MSDN) Web site: For
more information about how to use the NotifyIcon class to display an icon for
an application in the notification area, visit the following MSDN Web
site:
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbHOWTOmaster kbcode KB903898 kbAudDeveloper |
---|
|