Archive for the ‘Winforms’ Category.

C# Winforms: Creating a Application which Minimizes to a Tray Icon

(Updated 02/14/2012 Code Examples use SyntaxHighlighter)

The goal is to having a working application which hides itself not to the taskbar but to an icon in the system tray. This is valid in .Net 2 and above. Here are the steps

  1. Create a Winform Application.
  2. Bring up the mainform.
  3. Drag a NotifyIcon component to the form.

NotifyIcon Setup

  1. In the properties set the icon to use. Note if you don’t have one grab the root.ico from the Visual Studio folder to use.
  2. We want to hide the icon initially so set the Visible property to false.
  3. Add this code to the click event
private void notifyIcon1_Click( object sender, EventArgs e )
    WindowState = FormWindowState.Normal;

Winform Setup

  1. Add this code to the resize event and you are done, it handles the states of the icon and taskbar button.
private void Form1_Resize( object sender, EventArgs e )
   if (WindowState == FormWindowState.Minimized)
      ShowInTaskbar       = false;
      notifyIcon1.Visible = true;
      ShowInTaskbar       = true;
      notifyIcon1.Visible = false;

At this point compile and run. The application starts of as normal with a taskbar button but no icon. When the application is minimized it goes to the tray icon and there is no taskbar button. When the user clicks on the icon in the tray, it is returned to its previous state and the icon is removed.