开发者

Having a menu open when you click on a NotifyIcon (VB.NET)

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.


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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜