Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put an .net application in system tray when minimized?

can anyone please suggest a good code example of vb.net/c# code to put the application in system tray when minized.

like image 633
bugBurger Avatar asked Sep 16 '08 19:09

bugBurger


2 Answers

Add a NotifyIcon control to your form, then use the following code:

    private void frm_main_Resize(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
           this.ShowInTaskbar = false;
           this.Hide();
           notifyIcon1.Visible = true;
        }
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = true;
        notifyIcon1.Visible = false;
    }

You may not need to set the ShowInTaskbar property.

like image 62
Phillip Wells Avatar answered Oct 02 '22 15:10

Phillip Wells


You can leverage a built in control called NotifyIcon. This creates a tray icon when shown. @Phillip has a code example that is somewhat complete.

There is a gotcha though:

You must override your applications main form Dispose method to call Dispose on NotifyIcon, otherwise it will stay in your tray after application exits.

public void Form_Dispose(object sender, EventArgs e)
{
   if (this.Disposing)
      notifyIcon1.Dispose();
}

Something like that.

like image 32
FlySwat Avatar answered Oct 02 '22 15:10

FlySwat