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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With