The NotifyIcon Control
- Details
- Written by David Corrales
- Last Updated: 15 June 2016
- Created: 08 May 2012
- Hits: 18323
NotifyIcon [System.Windows.Forms.NotifyIcon]
Specifies a component that creates an icon in the notification area of the Window’s taskbar.
Default Event: MouseDoubleClick
Why use a NotifyIcon control?
Use the NotifyIcon control, to alert users of special events, such as when a task is completed. Typically this control is used for informational purposes, but it can also be used as a source for user interaction.
Note: When you add a NotifyIcon it will appear at the bottom of the Designer:
Important Properties:
BalloonTipText
This property sets the text associated with the balloon ToolTip. Use this property to display a message in the Balloon Tooltip.
Setting the BalloonTipText in the script editor:
$NotifyIcon.BalloonTipText ="This is the balloon text"
BalloonTipTitle
This property sets the title of the balloon ToolTip.
The title text is displayed above the balloon text.
BalloonTipIcon
This property sets the icon to associate with the balloon Tooltip. In other words, the icon displayed inside the tooltip itself.
Note: The BalloonTipTitle property must be set in order to view the balloon icon.
Values (Default: None):
None
No icon is displayed.
Info
An information icon.
Warning
A warning icon.
Error
An error icon.
Icon
This property sets the icon to display in the system tray.
Important: This property must be set; otherwise the tooltip balloon will not show!
The designer will allow you to browse and select an icon to display when the tooltip is shown.
Click on the browse button in the Property Panel:
Or use the menu in the designer:
Next select the Icon file
Finally the Icon will be displayed in the system tray:
Note: If a “phantom” icon remains in the system tray after closing the form, then it is recommended set the Visible property to False in order to clear the icon before closing form.
ContextMenuStrip
This property sets the shortcut menu to show when the user right-clicks the icon.
Set this property to an existing ContextMenuStrip in order to assign a menu to the system tray icon. See Spotlight on the ContextMenuStrip Control for more details.
Visible
This property indicates whether the icon is visible in the notification area of the taskbar.
Values (Default: True):
True / False
Important Events:
BalloonTipClicked
This event occurs when the balloon tip is clicked. Use this event to react to user clicks in the ToolTip balloon.
$notifyicon1_BalloonTipClicked={ Write-Host'The Balloon Tip was Clicked' }
Clicked & DoubleClicked
These events occur when the system tray icon is clicked. If you need more information such as which mouse button was used, then it is recommended to use the MouseClick events (See below).
MouseClick
This event occurs when a user clicks on the system tray icon.
[System.Windows.Forms.LabelEditEventHandler]
Properties Description Button Gets which mouse button was pressed. Clicks Gets the number of times the mouse button was pressed and released. Delta (Not applicable) Location Gets the location of the mouse during the generating mouse event. X Gets the x-coordinate of the mouse during the generating mouse event. Y Gets the y-coordinate of the mouse during the generating mouse event.
$notifyicon1_MouseClick=[System.Windows.Forms.MouseEventHandler]{ #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]Write-Host"System Tray Icon Mouse Click: $($_.Button) Clicks: $($_.Clicks)" }
MouseDoubleClick
These events occur when a user double clicks on a system tray icon. This event has the same arguments as the MouseClick event.
$notifyicon1_MouseDoubleClick=[System.Windows.Forms.MouseEventHandler]{ #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]Write-Host"System Tray Icon Mouse Double Click: $($_.Button) Clicks: $($_.Clicks)" }
Important Methods:
ShowBalloonTip
This method displays a balloon tip in the taskbar for the specified time period.
[void] ShowBalloonTip([int] timeout)
Note: The method uses the properties of the NotifyIcon to display the balloon tip. Therefore they must be set before calling this method.
$NotifyIcon.ShowBalloonTip(0)
[void] ShowBalloonTip([int] timeout, [string] tipTitle, [string] tipText, [ToolTipIcon] tipIcon)
Note: This variation, displays a balloon tip with the specified title, text, and icon in the taskbar for the specified time period. You need not set the NotifyIcon’s properties if you use this method variation.
$NotifyIcon.ShowBalloonTip($Timeout, $BalloonTipTitle, $BalloonTipText, $BalloonTipIcon)
Helper Function:
The following is a helper function that allows you to display the NotifyIcon. The help function also assigns the calling executable’s icon, if the NotifyIcon’s Icon property hasn’t been assigned.
functionShow-NotifyIcon { <# .SYNOPSIS Displays a NotifyIcon's balloon tip message in the taskbar's notification area. .DESCRIPTION Displays a NotifyIcon's a balloon tip message in the taskbar's notification area. .PARAMETER NotifyIcon The NotifyIcon control that will be displayed. .PARAMETER BalloonTipText Sets the text to display in the balloon tip. .PARAMETER BalloonTipTitle Sets the Title to display in the balloon tip. .PARAMETER BalloonTipIcon The icon to display in the ballon tip. .PARAMETER Timeout The time the ToolTip Balloon will remain visible in milliseconds. Default: 0 - Uses windows default. #> param( [Parameter(Mandatory =$true, Position =0)] [ValidateNotNull()] [System.Windows.Forms.NotifyIcon]$NotifyIcon, [Parameter(Mandatory =$true, Position =1)] [ValidateNotNullOrEmpty()] [String]$BalloonTipText, [Parameter(Position =2)] [String]$BalloonTipTitle='', [Parameter(Position =3)] [System.Windows.Forms.ToolTipIcon]$BalloonTipIcon='None', [Parameter(Position =4)] [int]$Timeout=0 ) if($NotifyIcon.Icon -eq$null) { #Set a Default Icon otherwise the balloon will not show$NotifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon([System.Windows.Forms.Application]::ExecutablePath) } $NotifyIcon.ShowBalloonTip($Timeout, $BalloonTipTitle, $BalloonTipText, $BalloonTipIcon) }
Example Use:
Show-NotifyIcon-NotifyIcon$notifyicon1-BalloonTipText$textboxText.Text `-BalloonTipTitle$textboxTitle.Text -BalloonTipIcon$combobox1.SelectedItem
You can download the NotifyIcon Sample.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.