How can I close a NotifyIcon BallonToolTip from code?

1.2k views Asked by At

We are using a NotifyIcon to alert the user when they receive a new message that needs their attention. In the event that someone else gets to the message first, the notify icon should get hidden again, however I am having a problem with figuring out how to close the balloon from code behind.

My code looks something like this:

myNotifyIcon.ShowBalloonTip(2000, title, message, icon);

I have tried the suggestions found here, but none are suitable.

  • Using myNotifyIcon.Visible = true does not hide it

  • Using myNotifyIcon.Visible = false; myNotifyIcon.Visible = true; will hide it, but it also hides the icon in the tray and when it is shown again, it shows up a a different location.

  • myNotifyIcon.Show(0) is not a valid method

  • myNotifyIcon.ShowBalloonTip(0) or myNotifyIcon.ShowBalloonTip(1) does not appear to work as the balloon just gets shown and doesn't appear to go away on its own at all.

I read this question about using the WinAPI to find the window and send it a WM_CLOSE message, but I'm not too sure how to do that reliably.

How can I close a NotifyIcon from the code behind?

2

There are 2 answers

1
Nick Spreitzer On

I've never found a non-hacky way to do that. The documentation says:

Minimum and maximum timeout values are enforced by the operating system and are typically 10 and 30 seconds, respectively, however this can vary depending on the operating system. Timeout values that are too large or too small are adjusted to the appropriate minimum or maximum value.

Even though this does not specifically address the question of explicitly closing the balloon, it indicates to me that callers of the ShowBalloonTip() method simply do not have complete control over the balloon, once it's been shown.

The best option I've found is one you already mentioned:

myNotifyIcon.Visible = false;
myNotifyIcon.Visible = true;

Not ideal, but it works. Another idea would be to change the message displayed in the balloon to indicate that the previous message is obsolete:

myNotifyIcon.ShowBalloonTip(2000, "Title", "Never mind!", ToolTipIcon.Info);
0
stacy On

There is a simple way to do that.

myNotifyIcon.ShowBalloonTip(2000);
myNotifyIcon.visible = true;
this.Hide();

Try this, it'll definitely work 100%.