articles

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

System Tray with Notify Icon in C#.net

System Tray with Notify Icon in C#.net

Vijay Shukla 18789 18-Jun-2013

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

Notify Icon Component

The Notify Icon is used to add functionality for system tray notification functionality to a winform application.

Whenever an application is run, an icon will be added to the system tray. These icons are shown on the right side of the taskbar.

System Tray 

The system tray is placed on the right side of the windows taskbar (usually at the bottom next to the clock) and contains small icons for easy access to system functions like a fax machine, a printer, a modem, volume, and more.

The 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.

  • Create a project in visual studio 2010.
  • Drag and drop the notify icon from toolbox.

System Tray with Notify Icon in C#.net

3.)    Add a ContextMenuStrip the same as drag and drop from toolbox forgive the context menu on notifying 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 a .ico image in Icon property under the notifyicon1 properties neither notify icon will not show in taskbar 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 running your application you will see your selected icon show on the right side of taskbar but when you try to right-click on the notify icon then a context menu has 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: -
  private void FormMain_Load(object sender, EventArgs e)

 {
 notifyIcon1.Visible = true;
 }
Code description:-
  • 1.       This is a formation load event method.
  • 2.       Start form load event method {.
  • 3.       After loading the form main notifyIcon1 visibility is true and notify icon shown on the right side taskbar.
  • 4.       Close event method.
Form Resize event:-

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

 private void FormMain_Resize(object sender, EventArgs e)
 {
 if (WindowState == FormWindowState.Minimized)
 {
 this.ShowInTaskbar = false;
 }
 else if (WindowState == FormWindowState.Normal)
{
 this.ShowInTaskbar = true;
 }
 }
Code Description: -
  • 1.       This is a formation 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 the context menu strip event:-

This event fired when the user clicks on the show option from bonded ContextMenuStrip in notify Icon.

 private void Show_Click(object sender, EventArgs e)

{
 if (WindowState == FormWindowState.Minimized)
 {
 this.ShowInTaskbar = true;
 this.WindowState = FormWindowState.Normal;
}
}
Code Description: -

1.       This is a ContextMenuStrip Show option event method.

3.       Check the WindowState of formMain.

5.       This line sets the true in ShowInTaskbar property and formMain show on taskbar.

6.       This line of code sets 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.

 private void exitToolStripMenuItem_Click(object sender, EventArgs e)

 {
this.Close();
}
Code Description: -
  • 1.       This is a ContextMenuStrip Exit option event method.
  • 3.       This line closes the run demo project.

c# c# 
Updated 27-Dec-2019

Leave Comment

Comments

Liked By