How can I close a NotifyIcon BallonToolTip from code?
Asked Answered
F

2

6

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?

Flier answered 16/12, 2013 at 15:57 Comment(4)
Have you tried notifyIcon.Icon = null;Theologize
It is not supported by the native winapi function. The crude workaround is to dispose the NotifyIcon.Ladonna
@HighCore Yes sadly, I got a new job and it's working with WinForms. I hope to go back to WPF in the future at some point though :)Flier
@HansPassant Won't that get rid of the icon in the system tray altogether?Flier
G
1

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);
Galvanism answered 16/12, 2013 at 21:30 Comment(1)
The idea to show a message to indicate that the previous message is obsolete does not work. The message is queued, and not shown until the previous message already times out.Gelatin
S
0

There is a simple way to do that.

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

Try this, it'll definitely work 100%.

Strongroom answered 17/8, 2016 at 16:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.