Visual Basic.NET by Nitin Chopra
System Tray Icon
and Context Menus
Ok
now we need to specify the icon that is to be displayed. Also we
will associate the context menu, set the tooltip text on the icon.
Finally we will set the visible property of the NotifyIcon to
True (This step is very important otherwise icon won’t be displayed)
'
The Icon property sets the icon that will appear
'
in the systray for this application.
nIcon.Icon
= New Icon("myicon.ico")
'
The ContextMenu property sets the menu that will
' appear when the systray
icon is right clicked.
nIcon.ContextMenu
= Me.ContextMenu
' The Text property sets the text that
will be displayed,
' in a tooltip, when the mouse hovers
over the systray ' icon.
nIcon.Text = "My NotifyIcon Example"
nIcon.Visible
= True
Now you
can handle the events corresponding to either the icon or the menu
icon. For example:-
Private
Sub nIcon_DoubleClick(ByVal
sender As
Object, ByVal e As
System.EventArgs) Handles nIcon.DoubleClick
' Show the form when the user double
clicks on the icon.
' Set the WindowState to normal if the
form is minimized.
If (Me.WindowState
= FormWindowState.Minimized) Then
Me.WindowState = FormWindowState.Normal
End
If
' Activate the form.
Me.Activate()
End
Sub
Private
Sub menuItem1_Click(ByVal
sender As
Object, ByVal e As
System.EventArgs) Handles menuItem1.Click
'exit the application
MsgBox("Exiting Application")
End
End
Sub
On the
click event show the context menu at a position defined by you.
Private
Sub nIcon_Click(ByVal
sender As
Object, ByVal e
As System.EventArgs)
Handles nIcon.Click
ctxtMenu.Show(Me,
New System.Drawing.Point(300, 300))
End
Sub
ContextMenu.Show()
function is used to display the menu at a specified position on
the screen.
|