NotifyIcon ContextMenu and too many click events

I am using the NotifyIcon class to display a taskbar icon. The icon performs 2 functions - when the user left-clicks with the left mouse button, he must display the window, when the user right-clicks, he must display the context menu. This works fine, except for the window displayed after the user clicks a button in the context menu. Here is my code:

 contextMenuItems = new List<MenuItem>(); contextMenuItems.Add(new MenuItem("Function A", new EventHandler(a_Clicked))); contextMenuItems.Add(new MenuItem("-")); contextMenuItems.Add(new MenuItem("Function B", new EventHandler(b_Clicked))); trayIcon = new System.Windows.Forms.NotifyIcon(); trayIcon.MouseClick += new MouseEventHandler(trayIcon_IconClicked); trayIcon.Icon = new Icon(GetType(), "Icon.ico"); trayIcon.ContextMenu = contextMenu; trayIcon.Visible = true; 

The problem is that my trayIcon_IconClicked event fires when the user selects "Function A" or "Function B". Why is this happening?

Thanks J

+4
source share
2 answers

Assigning a context menu to a NotifyIcon control, it automatically right-clicks and opens the assigned context menu. If you want to execute some logic before the context menu is actually displayed, assign a delegate to the contextMenu.Popup event.

 ... contextMenu.Popup += new EventHandler(contextMenu_Popup); ... private void trayIcon_IconClicked(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { //Do something here. } /* Only do this if you're not setting the trayIcon.ContextMenu property, otherwise use the contextMenu.Popup event. else if(e.Button == MouseButtons.Right) { //Show uses assigned controls Client location to set position, //so must go from screen to client coords. contextMenu.Show(this, this.PointToClient(Cursor.Position)); } */ } private void contextMenu_Popup(object sender, EventArgs e) { //Do something before showing the context menu. } 

My guess about why the window appears is that the context menu that you open uses NotifyIcon as the target control, so when you click on it, it starts the click handler assigned by NotifyIcon.

Edit: Another option is to use ContextMenuStrip. NotifyIcon also has a ContextMenuStrip property, and it seems to have much more functionality associated with it (noticed that I could do more, programmable wise). You might want to do this if something is not working for some reason.

+3
source

I ran into the same problem. Changing NotifyIcon ContextMenu for ContextMenuStrip did not solve the problem (in fact, when I changed ContextMenu, the Click event occurs when ContextMenuStrip shows, and not when the user clicked on one of the elements.

My solution to the problem is to change the event that I used to display the left-click context menu. Instead of using the Click event handler, I use MouseUp and check which MouseButton clicked.

Building NotifyIcon (notifyContext is System.Windows.Forms.ContextMenuStrip)

 m_notifyIcon.MouseUp += new Forms.MouseEventHandler(m_notifyIcon_MouseUp); m_notifyIcon.ContextMenuStrip = notifyContext; Handling the left click event and show the main contextmenu: void m_notifyIcon_MouseUp(object sender, Forms.MouseEventArgs e) { if (e.Button == Forms.MouseButtons.Left) { mainContext.IsOpen = ! mainContext.IsOpen; } } 
0
source

All Articles