Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

NotifyIcon in System Tray with Context Menu Using C#

0.00/5 (No votes)
5 Oct 2007 1  
This is a small application which shows how we to use NotifyIcon in the system tray and also use a menu control with it.

Screenshot - systemTray.jpg

Introduction

Using the NotifyIcon component, we can put our application icon on the system tray and use a ConextMenuStrip to control the menu in the system tray.

Background

The function of the ConextMenuStrip is the similar to that of the menu bar. It will appear only when we right click for it.

Screenshot - conextmenu.jpg

Using the code

The code for this application is very simple, you just need to know how to use it. Code for restore application, open settings, close from system tray are listed in this section:

namespace NotifyIcon
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Use_Notify(); // Setting up all Property of Notifyicon 
        }

        private void Use_Notify()
        {
            MyNotify.ContextMenuStrip = contextMenuStrip1; 
            MyNotify.BalloonTipText = "This is A Sample Application";
            MyNotify.BalloonTipTitle = "Your Application Name";
            MyNotify.ShowBalloonTip(1); 
        }

        private void Form1_Resize(object sender, System.EventArgs e)
        {
            // Hide The Form when it's minimized
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }
        private void MyNotify_DoubleClick(object sender, System.EventArgs e)
        {
            // Show the form when Dblclicked on Notifyicon
            Show();
            WindowState = FormWindowState.Normal;
        }
        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Will Close Your Application 
            MyNotify.Dispose();
            Application.Exit();
        }

        private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Will Restore Your Application 
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Settings 1
            MessageBox.Show("Your Application Settings 1");
        }

        private void settings2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Settings 2
            MessageBox.Show("Your Application Settings 2");
        }
    }
}

You can apply this in any of your applications.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here