Home > DeveloperSection > Articles > System Tray with Notify Icon in C#.net

System Tray with Notify Icon in C#.net


C# C# 
Ratings:
0 Comment(s)
 9650  View(s)
Rate this:

System Tray with Notify Icon in C#.net

In this article I am trying to explain the concept of notify Icon control with System tray in C#.net winform.

Notify Icon Component

A Notify Icon is used to add functionality for system tray notification functionality to a winform application. When an application is run, an icon will be added to system tray. These icons are shows in right side of taskbar.

System Tray

The system tray is placed in the right side of windows taskbar (usually at the bottom next to the clock) and contains small icons for easy access to system functions such as fax, printer, modem, volume, and more. Double click or right click on an icon to view and access the details and controls.

Implement Notify Icon Component

I am trying to make a small demo for related notify icon and system tray concept. Below steps for create a demo.

1.       Create a project in visual studio 2010.

2.       Drag and drop the notify icon from toolbox.

System Tray with Notify Icon in C#.net

3.       Add a ContextMenuStrip same as drag and drop from toolbox for give the context menu on notify Icon.

System Tray with Notify Icon in C#.net

 

4.       Bind the ContextMenuStrip in notifyiconright click on the notify icon to open properties Goto on ContextMenuStrip property and select the contextStripMenu1.

System Tray with Notify Icon in C#.net

(Note: - You need to set an .ico image in Icon property under the notifyicon1 properties neither notify icon will not show in task bar because by default icon image is not set.)

 

5.       Now in form load event u just type the notifyIcon1.Visible = true;and run your application after run your application  you will see your selected icon show on the right side of task bar but when you trying to right click on the notify icon then a context menu is appeared with Show and Exit option but it will not work because code is not there in its event.

Below I am trying to describe my demo project code:-

Form load event: -

1.  private void FormMain_Load(object sender, EventArgs e)

2.  {

3.  notifyIcon1.Visible = true;         

4.  }

Code description:-

1.       This is formMain load event method.

2.       Start form load event method {.

3.       After loading the form main notifyIcon1 visibility is true and notify icon show on the  right side taskbar.

4.       Close event method.

Form Resize event:-

This event use for the check form is minimized or maximized.

1.  private void FormMain_Resize(object sender, EventArgs e)

2.  {

3.  if (WindowState == FormWindowState.Minimized)

4.  {

5.  this.ShowInTaskbar = false;

6.  }

7.  else if (WindowState == FormWindowState.Normal)

8.  {

9.  this.ShowInTaskbar = true;

10. }

11. }

Code Description: -

1.       This is formMain Resize event method.

3.       Check the Window State of form.

5.       If Window State is minimized then execute this line and this line set form show in taskbar false.

7.       Check the Window State of form.

9.       If step 6 is true then this line will execute and this line set form show in taskbar true.

Show context menu strip event:-

This event fired when user click on the show option from bonded ContextMenuStrip in notifies Icon.

1.  private void Show_Click(object sender, EventArgs e)

2.  {

3.  if (WindowState == FormWindowState.Minimized)

4.  {

5.  this.ShowInTaskbar = true;

6.  this.WindowState = FormWindowState.Normal;

7.  }

8.  }

Code Description: -

1.       This is ContextMenuStrip Show option event method.

3.       Check the WindowState of formMain.

5.       This line set the true in ShowInTaskbar property and formMain show on task bar.

6.       This line of code set the WindowState normal in formMain (It will show formMain).

Exit context menu strip event:-

This event fired when user click on Exit option in ContextMenuStrip.

1.  private void exitToolStripMenuItem_Click(object sender, EventArgs e)

2.  {

3.  this.Close();

4.  }

Code Description: -

1.       This is ContextMenuStrip Exit option event method.

3.       This line closes the run demo project.


Don't want to miss updates? Please click the below button!

Follow MindStick