views:

610

answers:

2

I want to have a NotifyIcon in the system tray that when clicked, opens a context menu on the NotifyIcon with several options that open different forms.

I have read I need to use a ContextMenu and after Google'ing and trying out various code I can't seem to get it working :/

Any help on the matter is greatly appreicated.

+1  A: 

Have you already designed your ContextMenu in the form designer? If you haven't, that's the first step. Create a new ContextMenu (by just doubleclicking it in the toolbox on the left) and add all your menu items to it. Then you can double click each item which will put in an empty .Click event handler which is where you'll put in the formname.Show() call. You will also have to add in a NotifyIcon in the form designer and when you edit the properties for it, there is a property called "Context Menu" where you'll enter the name of the above ContextMenu. If you want the icon to be visible all the time, you can just set the Visible property to True.

sentry07
+1  A: 

Add context menu to your for (if there is a form) or declare a context menu instance:

Here is a quick example:

ContextMenu cm; NotifyIcon ni;

public Form1() { ni = new NotifyIcon(); ni.BalloonTipIcon = ToolTipIcon.Info;

    cm = new ContextMenu();

    cm.MenuItems.Add(new MenuItem("Hello", delegate(object sender, EventArgs e)
    {
        MessageBox.Show(DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt"));
    }));

    cm.MenuItems.Add(new MenuItem("Exit", delegate(object sender, EventArgs e)
    {
        ni.Visible = false;
        ni.Dispose();
        Application.Exit();
    }));

    ni.ContextMenu = cm;

    ni.Visible = true;
    ni.Icon = this.Icon;
}
TheVillageIdiot