Probably my question is duplicate of this Multiple icons open in tray bar. In my winforms application I'm showing the Application in the system tray once the form is closed ie the application doesnt exit after closing the form but exits on clicking "Close" on the Right Click Context menu on the system tray of the Applicaion.
But as I go on with using the application I notice that there are many more Notification Icon's in the system tray. But once I mouse hover over them they all disappear except the one with the application running. I've tried every method to eliminate the multiple icon's but I'm not able to do so.
Below is my code For Minimizing to System Tray
public void MinimizeToTray()
{
try
{
this.WindowState = FormWindowState.Minimized;
TrayIcon.Visible = true;
TrayIcon.ShowBalloonTip(1000);
ShowInTaskbar = false;
//this.Activate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
On the form load I have added this code
private void LoadTrayMenu()
{
TrayMenu.Items.Add("Reminder");
TrayMenu.Items.Add("Close");
TrayMenu.Items[0].Click += new EventHandler(this.Reminder_Click);
TrayMenu.Items[1].Click += new System.EventHandler(this.Dispose_Click);
TrayIcon.ContextMenuStrip = TrayMenu;
}
The dispose event is as follows
private void Dispose_Click(object Sender, EventArgs e)
{
TrayIcon.Visible = false;
TrayIcon.Icon = null;
TrayIcon.Dispose();
this.Dispose();
}
On the mouse click of the Icon I have written the following code
private void TrayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Show();
this.WindowState = FormWindowState.Normal;
TrayIcon.Visible = false;
//TrayIcon.Icon = null;
//TrayIcon.Dispose();
ShowInTaskbar = true;
}
}
I tried Clearing the Notify Icons, but even that didn't help me. Am I missing something really obvious. Any help would be appreciated.