I am trying to use a contextmenustrip with a notifyicon. When I right click on the notifyicon, the contextmenustrip does not show. I haven't found any solutions on the web. Here is my code:
public class Program
{
static ContextMenuStrip contextMenuStrip1;
static ToolStripMenuItem exitToolStripMenuItem;
static ToolStripMenuItem restoreToolStripMenuItem;
static IContainer components;
static NotifyIcon notifyIcon1;
static win window = new win();
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Intialise();
Application.Run();
}
static void Intialise()
{
components = new Container();
contextMenuStrip1 = new ContextMenuStrip(components);
restoreToolStripMenuItem = new ToolStripMenuItem();
exitToolStripMenuItem = new ToolStripMenuItem();
notifyIcon1 = new NotifyIcon(components);
contextMenuStrip1.SuspendLayout();
//
// restoreToolStripMenuItem
//
restoreToolStripMenuItem.Name = "restoreToolStripMenuItem";
restoreToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
restoreToolStripMenuItem.Text = "Restore";
restoreToolStripMenuItem.Click += new EventHandler(RestoreToolStripMenuItem_Click);
//
// exitToolStripMenuItem
//
exitToolStripMenuItem.Name = "exitToolStripMenuItem";
exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
exitToolStripMenuItem.Text = "Exit";
exitToolStripMenuItem.Click += new EventHandler(ExitToolStripMenuItem_Click);
//
// contextMenuStrip1
//
contextMenuStrip1.Items.AddRange(new ToolStripItem[] {
restoreToolStripMenuItem,
exitToolStripMenuItem});
contextMenuStrip1.Name = "contextMenuStrip1";
contextMenuStrip1.RenderMode = ToolStripRenderMode.System;
contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
contextMenuStrip1.Text = "File";
//
// notifyIcon1
//
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
notifyIcon1.Icon = Properties.Resources.stm2;
notifyIcon1.Text = "Screen Time Monitor";
notifyIcon1.Visible = true;
notifyIcon1.MouseUp += new MouseEventHandler(NotifyIcon1_MouseUp);
notifyIcon1.MouseDoubleClick += new MouseEventHandler(NotifyIcon1_MouseDoubleClick);
}
private static void NotifyIcon1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon1, null);
}
else if(e.Button == MouseButtons.Right)
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon1, null);
}
}
}
I have tried using the mouseUp event to open the contextmenu but the contextmenu does not show either. I have seen code where people write notifyIcon1.ContextMenuStrip = contextMenuStrip1;
and that works. Though it does not work for me. Thanks.
The line contextMenuStrip1.SuspendLayout();
stopped the contextmenustrip from showing. I had copied the code from a generated windows form, so I don't know why that line of code was in there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.