1

after the Ballon Tip of my NotifyIcon has been close the icon still stays in tray. It vanishes only when I hover it with my mouse cursor.

I have a class Notification.cs which has one component - NotifyIcon.

I call it from my Console Application as I only want to show the notification when certain conditions are fulfilled.

Some code:

  • how I call the notification from within my program:

    Notification not = new Notification("Error occured, do this or that", "Some error", System.Windows.Forms.ToolTipIcon.Error);
    not.getIcon().ShowBalloonTip(1000);
    
  • the notification class:

    public Notification(string baloonTipText, string baloonTipTitle, System.Windows.Forms.ToolTipIcon icon) : this()
    {
        this.icon.Visible = true;
        this.icon.BalloonTipText = baloonTipText;
        this.icon.BalloonTipTitle = baloonTipTitle;
        this.icon.BalloonTipIcon = icon;
    }
    
    public System.Windows.Forms.NotifyIcon getIcon()
    {
        return this.icon;
    }
    
    private void icon_BalloonTipClosed(object sender, EventArgs e)
    {
        this.icon.Visible = false;
        this.icon.Dispose();            
    }
    

Any ideas?

j0k
  • 22,600
  • 28
  • 79
  • 90
wojtuch
  • 188
  • 2
  • 11
  • I asked [the same question](http://stackoverflow.com/questions/10980029/notify-icon-stays-in-system-tray-on-application-close) a while ago and got no answers but a few helpful comments – JMK Oct 19 '12 at 11:24
  • @JMK I'm not so sure this is the same question. The way I read this question, the program is still running. –  Oct 19 '12 at 11:35
  • What `type` is the `Notification` class? – Mike Perrenoud Oct 19 '12 at 11:48
  • @JMK the link was helpful, I can now get rid of the notifications ([http://safari-tech.serveblog.net/?p=101]) but the problem is that call to the method `RefreshTaskbarNotificationArea()` in `icon_BalloonTipClosed` method doesn't work for me. I have to call it externally, which is not a good option @hvd yes, the main program is still running - I instantiate the Notification class, get it's icon and show the balloon tip only to tell the user sth went wrong @BigM I don't really understand your question. It's a class I created in VS using the Component Class Template – wojtuch Oct 19 '12 at 12:41

2 Answers2

3

Because you're running in a console application, your icon_BalloonTipClosed handler will not be invoked when the balloon tip is closed (no message pump). You will need to call Dispose manually either as your application exits or set a timer (System.Threading.Timer, System.Windows.Forms.Timer will not work) with a timeout longer than the balloon tip's timeout. e.g.:

timer = new Timer(state => not.getIcon().Dispose(), null, 1200, Timeout.Infinite);
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
0
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace ShowToolTip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btBallonToolTip_Click(object sender, EventArgs e)
        {
            ShowBalloonTip();
            this.Hide();
        }

        private void ShowBalloonTip()
        {
            Container bpcomponents = new Container();
            ContextMenu contextMenu1 = new ContextMenu();

            MenuItem runMenu = new MenuItem();
            runMenu.Index = 1;
            runMenu.Text = "Run...";
            runMenu.Click += new EventHandler(runMenu_Click);

            MenuItem breakMenu = new MenuItem();
            breakMenu.Index = 2;
            breakMenu.Text = "-------------";

            MenuItem exitMenu = new MenuItem();
            exitMenu.Index = 3;
            exitMenu.Text = "E&xit";

            exitMenu.Click += new EventHandler(exitMenu_Click);

            // Initialize contextMenu1
            contextMenu1.MenuItems.AddRange(
                        new System.Windows.Forms.MenuItem[] { runMenu, breakMenu, exitMenu });

            // Initialize menuItem1

            this.ClientSize = new System.Drawing.Size(0, 0);
            this.Text = "Ballon Tootip Example";

            // Create the NotifyIcon.
            NotifyIcon notifyIcon = new NotifyIcon(bpcomponents);

            // The Icon property sets the icon that will appear
            // in the systray for this application.
            string iconPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\setup-icon.ico";
            notifyIcon.Icon = new Icon(iconPath);

            // The ContextMenu property sets the menu that will
            // appear when the systray icon is right clicked.
            notifyIcon.ContextMenu = contextMenu1;

            notifyIcon.Visible = true;

            // The Text property sets the text that will be displayed,
            // in a tooltip, when the mouse hovers over the systray icon.
            notifyIcon.Text = "Morgan Tech Space BallonTip Running...";
            notifyIcon.BalloonTipText = "Morgan Tech Space BallonTip Running...";
            notifyIcon.BalloonTipTitle = "Morgan Tech Space";
            notifyIcon.ShowBalloonTip(1000);
        }

        void exitMenu_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void runMenu_Click(object sender, EventArgs e)
        {
            MessageBox.Show("BallonTip is Running....");
        }
    }
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Kevin M
  • 5,436
  • 4
  • 44
  • 46