[2005] How do I work with the NotifyIcon object?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: [2005] How do I work with the NotifyIcon object?

Hybrid View

  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    [2005] 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:
    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 :
    Code:
            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:
    Code:
            NotifyIcon1.Visible = True 'Show
    Q: How do I get Balloon Style tips?

    A: There are various properties you can set:
    • BalloonTipIcon
    • BalloonTipText
    • BalloonTipTitle


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

    A: You can set the BalloonTipIcon property to any of the following:
    • Error
    • Info
    • Warning
    • None


    You can set the BalloonTipIcon property in the Properties Window, or with code:
    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:
    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:
    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:
    Code:
    NotifyIcon1.ShowBalloonTip(5000) 'Display 5 Seconds
    You can also call the ShowBalloonTip method like this :
    Code:
    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 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:
      Code:
          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:
      Code:
          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:
      Code:
          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
      Code:
          Private blnExit As Boolean ' Are we exiting
    • Edit the Form1_FormClosing event to look like the following:
      Code:
          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:
      Code:
          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:
      Code:
          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
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center