# CodeGuru Technical FAQs > CodeGuru Individual Visual Basic FAQs >  [2005] How do I work with the NotifyIcon object?

## HanneSThEGreaT

*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 modeSelect the *Icon* PropertyBrowse to selected icon.
You can also add an icon to the Notifyicon object with code:


```
        NotifyIcon1.Icon = New System.Drawing.Icon(Application.StartupPath & "\misc01.ico") 'Load icon
```

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.  
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 :


```
        NotifyIcon1.Text = "Demonstration" & Environment.NewLine & "This is a test" 'Multiple Line ToolTip Text
```

*Q:* How do I display the Notifyicon?

*A:* Set the *Visible* property to True, in the properties Window, or through code:


```
        NotifyIcon1.Visible = True 'Show
```

*Q:* How do I get Balloon Style tips?

*A:* There are various properties you can set:
BalloonTipIconBalloonTipTextBalloonTipTitle

*Q:* What icons can I set with the *BalloonTipIcon* property and how?

*A:* You can set the *BalloonTipIcon* property to any of the following:
ErrorInfoWarningNone

You can set the *BalloonTipIcon* property in the Properties Window, or with code:


```
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Error 'Set Error Icon
'Or
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info 'Set Info Icon
'Or
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning 'Set Warning Icon
'Or
NotifyIcon1.BalloonTipIcon = ToolTipIcon.None 'No Icon
```

*Q:* How do I set the *BalloonTipText* property?

*A:* You can edit the *BalloonTipText* property in the Properties Window, or through code:


```
NotifyIcon1.BalloonTipText = "This is a test" 'Set Balloon Tip Text
```

*Q:* How do I set the *BalloonTipTitle* property?

*A:* You can set it throught the *BalloonTipTitle* property in the Properties Window, or through code:


```
        NotifyIcon1.BalloonTipTitle = "Demonstration" 'Set Title
```

*Q:* Why doesn't the BalloonTip show yet?

*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:


```
NotifyIcon1.ShowBalloonTip(5000) 'Display 5 Seconds
```

You can also call the ShowBalloonTip method like this :


```
NotifyIcon1.ShowBalloonTip(3000, "Demonstration", "This is a Test", ToolTipIcon.None) 'Display 3 seconds, show the title, show the text, and the icon
```

*Q:* Can I show alternating icons for my Notifyicon?

*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 WindowAdd 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
        Else 'Left
            Timer1.Enabled = False
        End If
    End Sub
```

In the *Timer1_Tick* event, add the following:


```
    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
        End Select
        imIndex += 1
    End Sub
```

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.

*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

        End If

    End Sub
```



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
        End If
    End Sub
```

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
    End Sub
```

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
        Me.Close() 'exit
    End Sub
```



*Q:* Can I have an example on the useage of the Notifyicon?

*A:* Have a look at the attachment

----------

