June 4th, 2007, 10:47 AM
 How do I work with the NotifyIcon object?
Q: What is a Notify icon?
A: The Windows Forms NotifyIcon component is typically used to display icons for processes that run in the background and do not show a user interface much of the time. An example would be a virus protection program that can be accessed by clicking an icon in the status notification area of the taskbar.
Q: How do I add a NotifyIcon to my form?
A: In the Toolbox, Make sure the All Windows Forms tab is selected, and scroll down until you find Notifyicon.
Q: What are the properties I can set for the Notifyicon?
A: NotifyIcon Members
Q: How do I set an Icon for the Notifyicon object?
A: Follow these simple steps:
- Switch to design mode
- Select the Icon Property
- Browse to selected icon.
You can also add an icon to the Notifyicon object with code:
The above code assumes that we have an icon file in our \Bin directory. Usually, you should include the full path to your icon file.
NotifyIcon1.Icon = New System.Drawing.Icon(Application.StartupPath & "\misc01.ico") 'Load icon
Having set this ( Icon ) property, through code or the Properties window, ensures that the icon gets displayed. If this property is not set, you will not see an icon in the taskbar's system tray.
Q: How do I set the text to be displayed on the Notifyicon?
A: You can set the text through the Text property in the properties window, or during runtime :
Q: How do I display the Notifyicon?
NotifyIcon1.Text = "Demonstration" & Environment.NewLine & "This is a test" 'Multiple Line ToolTip Text
A: Set the Visible property to True, in the properties Window, or through code:
Q: How do I get Balloon Style tips?
NotifyIcon1.Visible = True 'Show
A: There are various properties you can set:
Q: What icons can I set with the BalloonTipIcon property and how?
A: You can set the BalloonTipIcon property to any of the following:
You can set the BalloonTipIcon property in the Properties Window, or with code:
Q: How do I set the BalloonTipText property?
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Error 'Set Error Icon
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info 'Set Info Icon
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning 'Set Warning Icon
NotifyIcon1.BalloonTipIcon = ToolTipIcon.None 'No Icon
A: You can edit the BalloonTipText property in the Properties Window, or through code:
Q: How do I set the BalloonTipTitle property?
NotifyIcon1.BalloonTipText = "This is a test" 'Set Balloon Tip Text
A: You can set it throught the BalloonTipTitle property in the Properties Window, or through code:
Q: Why doesn't the BalloonTip show yet?
NotifyIcon1.BalloonTipTitle = "Demonstration" 'Set Title
A: The reason why the Balloon Tip does not show yet, is because we need to call the ShowBalloonTip method of the Notifyicon object in order to show the Balloon tip. You can call the ShowBalloonTip method like this:
You can also call the ShowBalloonTip method like this :
NotifyIcon1.ShowBalloonTip(5000) 'Display 5 Seconds
Q: Can I show alternating icons for my Notifyicon?
NotifyIcon1.ShowBalloonTip(3000, "Demonstration", "This is a Test", ToolTipIcon.None) 'Display 3 seconds, show the title, show the text, and the icon
A: Yes, you can. Many applications frequently change the notify icon, to explain what has happened, or that a certain task has completed. A perfect example would be an antivirus application. In order to achieve the same functionality, follow these steps:
- Add a Notifyicon object to your form ( if there isn't one already )
- Add an ImageList to your form, and add 3 pictures, by using the Images property in the properties Window
- Add a Timer to your form.
- Set the Timer's Enabled property to False and set it's Interval to 500 ( half a second )
- In the Code Window, edit the following code in the NotifyIcon1_MouseClick event:
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then 'If Right Clicked
Timer1.Enabled = True
Timer1.Enabled = False
- In the Timer1_Tick event, add the following:
Once we right click on the displayed Notifyicon, the Timer will be enabled, and the above code will execute. The icon will change to a different icon every half second.
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static imIndex As Short 'keeps track of current pic
Select Case imIndex
Case 0 'First Pic
Dim bm As Bitmap = ImageList1.Images(0)
NotifyIcon1.Icon = System.Drawing.Icon.FromHandle(bm.GetHicon) 'HIcon = Handle To Icon
Case 1 'Second Pic
Dim bm As Bitmap = ImageList1.Images(1)
NotifyIcon1.Icon = System.Drawing.Icon.FromHandle(bm.GetHicon)
Case 2 'Third Pic
Dim bm As Bitmap = ImageList1.Images(2)
NotifyIcon1.Icon = System.Drawing.Icon.FromHandle(bm.GetHicon)
imIndex = 0 'Start Over
imIndex += 1
Q: Can we change the Notifyicon's icon during runtime?
A: Yes, you can, follow these steps:
- Add a OpenFileDialog to your form.
- In the Code Window, add the following code to the NotifyIcon1_MouseDoubleClick event:
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
OpenFileDialog1.DefaultExt = ".ico" 'Show Ico files
OpenFileDialog1.Filter = "*.ico|*.ico" 'Only Icon files
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 'if something selected
NotifyIcon1.Icon = New Icon(OpenFileDialog1.FileName) 'Load New Icon From OFD
When the above code is run, we will able to double click on the displayed notifyicon, in order to browse to the icon we want to select.
Q: How can I still show my Notifyicon on the system tray, when I clicked "close" on my Form?
A: By doing this, your application is not really closed. You must now remember to provide different means of closing your application; for example: An Exit menu item, or an Exit button. You would also need to include a Boolean flag to indicate whether we are really exiting the application or not. To achieve this functionality, follow these steps:
- Add a Boolean variable in General Declarations
Private blnExit As Boolean ' Are we exiting
- Edit the Form1_FormClosing event to look like the following:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not blnExit Then
e.Cancel = True 'If this is not set, the form WILL close!
NotifyIcon1.Visible = True 'show icon
WindowState = FormWindowState.Minimized 'minimise our form
ShowInTaskbar = False 'remove form from taskbar
- Edit the NotifyIcon1_Click event to look like the following:
Private Sub NotifyIcon1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.Click
WindowState = FormWindowState.Normal 'show our form again
NotifyIcon1.Visible = False 'hide the notify icon
ShowInTaskbar = True 'show our form in the taskbar again
- In order to exit the application completely, we need to set the blnExit variable to True in the Exit menu item, or Exit button:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
blnExit = True 'We are really closing
Q: Can I have an example on the useage of the Notifyicon?
A: Have a look at the attachment
Click Here to Expand Forum to Full Width