Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to pop-up a context menu strip attached with a notification icon in the system tray for both left and right mouse click. Right click of the mouse on the icon works fine.

Issue I have with the left click. I did the following to achieve that.

C#
void notificationIcon_MouseClick(object sender, MouseEventArgs e)
{
     if (e.Button == MouseButtons.Left)
     {
          notificationIcon.ContextMenuStrip = new ContextMenus().Create();
          notificationIcon.ContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y);
     }
}


What happen was, once I click on the icon menu not appear on top of the mouse pointer, but at the bottom of the mouse pointer. And also I can see the menu title on the task bar. Any idea why is that happen?

Thanks
Posted
Comments
Mitchell J. 2-Apr-14 6:28am    
So what's the difference in the code between left-click and right click?
CodingLover 2-Apr-14 12:33pm    
Difference is in right-click the context menu appear as expected. But on left-click, as above, didn't show up properly. Actually it showed up with task bar notification. Is that clear? I'll post an image of two cases.
Mitchell J. 2-Apr-14 16:54pm    
An image might be helpful, but what I meant was "what code have you used to show the menu on the right click?" How areyou making it appear with a right click?
CodingLover 2-Apr-14 23:39pm    
I didn't do anything for the right-click, since it's automatically handled by the framework it self.
Mitchell J. 3-Apr-14 4:42am    
I tried your code in a small project myself. I think the behavior you are describing is default - not sure it can be changed. :-(

1 solution

I had a bit of a play with this and came up with the following idea...

On your notificationIcon, do not set the ContextMenuStrip property (i.e. you won't get the default behaviour)

Then try putting the code to show your context menu in the MouseUp event on the notificationIcon. For example ...
C#
private void notificationIcon_MouseUp(object sender, MouseEventArgs e)
{
      ContextMenuStrip cm = new ContextMenus().Create();
      cm.Show(Cursor.Position.X, Cursor.Position.Y);

      //the line below is how I was playing with a dummy menu
      //this.contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
}

You might have to fiddle with it a bit if you want different menus depending on which button was used.

As to the bit where the menu name is showing in the task bar - make sure you don't have the option "Always show all icons and notifications on the taskbar" ticked in Control Panel\All Control Panel Items\Notification Area Icons
 
Share this answer
 
Comments
CodingLover 21-Apr-14 7:44am    
Thanks a lot for the comment. I'll have a look.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900