NotifyIcon icon still visible after closing form

I”ve been playing around with the NotifyIcon class within C# recently but been having one major issue with it.  The class itself allows you to place an icon in the systray (down by the clock) and for it to display the standard balloon tip dialog.

However, the problem I was having is that when my calling parent closed my NotifyIcon would still be in my SysTray.  The only time it was removed was when I hovered over the icon with my mouse, then it would automatically disappear. Not great from the users point of view!

The MSDN page says nothing about this however I did find a useful forum post on the subject.  The solution is to manually call dispose and more importantly, set the variable to null.

The code is shown below, the notifyIcon variable is a member variable (private NotifyIcon m_notify;)

protected override void Dispose(bool disposing)
{
    if(m_notify != null)
    {
        m_notify.Visible = false;
        m_notify.Dispose();
        m_notify = null;
    }

    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

After inserting this code, when my parent form is closed the notify icon also disappears.  This took me a while to find out about, hopefully I have saved you some time.

Technorati Tags:

5 thoughts on “NotifyIcon icon still visible after closing form”

  1. You actually don’t need to set it non-visible first .. just dispose() and set to null/nothing (I’m a VB guy 🙂 does the trick for me in my Form Closing event.

    But other than that .. THX 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *