Click here to Skip to main content
15,793,546 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I'm working on a C# winForm app that has a ContextMenuStrip that adds toolStripMenuItems dynamically. I also have an icon in my resource file that I assign to the toolStripMenuItem.Image property and I handle the toolStripMenuItem.Click event. I actually want to handle the click event if the Image (icon) is clicked but Icon/Image does not have a click event. How do create a click icon that can assigned to the image property of the toolStripMenuItem?

Thanks in advance

CHill60 25-Apr-13 4:25am    
If you click on the image associated with a toolStripMenuItem then the toolStripMenuItem_Click event is fired
d.allen101 25-Apr-13 5:03am    
i don't want to fire the event based on toolStripMenuItem click event to fire the event when the icon is clicked.

1 solution

ContextMenuStrip ctxt = new ContextMenuStrip();

ToolStripMenuItem tsmi = new ToolStripMenuItem();
tsmi.Name = "Hello World";
tsmi.Image = Properties.Resources.MyIcon.ToBitMap();
tsmi.MouseUp += tsmi_MouseUp;


private void tsmi_MouseUp(object sender, MouseEventArgs e)
    ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
    Rectangle iconRect = new Rectangle(e.Location, tsmi.Image.Size);

     if (iconRect.Contains(e.Location))
          // do whatever you need to do here...
Share this answer

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