|
NotifyIcon
Notify Icons display an icon in Windows System Tray. This is really useful for processes
that run in the background and don't have their own interface. Since VB allows us
to create Windows Services (services that run in the background and display control
panels) now, we can use these notify icon's to associate funtionality to windows services.
You can also use this icon to associate help with your application, launch another
application or anything else which you think can be appropriate.
Notable properties of Notify Icon:
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
Notify Icon Event
The default event associated with Notify Icon is the MouseDown event which looks like
this in code:
Private Sub NotifyIcon2_MouseDown(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles NotifyIcon2.MouseDown
End Sub
|
You can also handle click and double-click events for notify icon. The code
sample below works with the click event of the Notify Icon to display a help file.
To create a Notify Icon component you need an icon (.ico) file to assign to it's Icon
property. If you have an icon then you can use it else you might need to create an
icon. You can create new icons with Visual Studio's icon designer. To open the icon
designer select Project->Add New Item and from the Add
New Item dialog select Icon File and click open. You
can use the toolbars that are visible to design your icon. The Icon Designer Window
is displayed below.
Sample Code
Drag a Notify Icon component and a Label control from the toolbar onto the form.
Open the properties window for the Notify Icon and set the Icon property to the path
of the icon and the text property to "Help with this Form". This is the icon that
will be displayed when you run the application. The Label control is needed to
set the help file. Set the text for label as "I have Help". The form in design view
should look like the image below.
This sample code launches a help file when you click the Icon in System Tray. This
sample code assumes that you have a help file, "Help.htm" in the C: drive of your
machine.
Private Sub NotifyIcon1_click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles NotifyIcon1.Click
'handling click event of the NotifyIcon
Help.ShowHelp(Label1, "c:\help.htm")
'using the Help class and it's ShowHelp method to display a help file
End Sub
|
When you run the application, an icon will be visible in the System Tray and when
you click the icon the help file named "Help.htm" will be launched. The image below
displays the output from above code.
|