Trouble with contextmenustrip


I'm having a problem with a context menu with subitems populated dynamically as below (for info only, I don't think it helps).
I would like to use one common routine to handle the clicked event, independently from which item or subitem was clicked. I tried to add an handler to contextmenustrip1_clickeditem to the subitems (as in maFlag(0) below) but it doesn't work, and I can't find a way that can handle items and subitems together.

In the event, I need access to ContextMenuStrip1.SourceControl and ContextMenuStrip1.SourceControl.FindForm() and the clicked item name.

What is the easiest way to achieve this?

Many thanks in advance,
Dim menuArray(3) As ToolStripMenuItem  'main menu items
menuArray(0) = New ToolStripMenuItem("Add", Nothing, Nothing, "add")
menuArray(1) = New ToolStripMenuItem("Edit", Nothing, Nothing, "edit")
menuArray(2) = New ToolStripMenuItem("Delete", Nothing, Nothing, "delete")
menuArray(3) = New ToolStripMenuItem("Flag", Nothing, Nothing, "flag")
Dim maFlag(3) As ToolStripMenuItem  ' flag's subitems
maFlag(0) = New ToolStripMenuItem("Attention Required", Nothing, AddressOf ContextMenuStrip1_ItemClicked, "attention")
maFlag(1) = New ToolStripMenuItem("Expired", Nothing, Nothing, "expired")
maFlag(2) = New ToolStripMenuItem("Incomplete", Nothing, Nothing, "incomplete")
maFlag(3) = New ToolStripMenuItem("Invalid", Nothing, Nothing, "invalid")

Open in new window

Who is Participating?
cdaly33Connect With a Mentor Commented:
In the ContextMenuStrip1_ItemClicked change the argument type from ToolStripMenuClickedItemEventArgs to EventArgs.
What about creating a generic function to handle the click event and attach the event using AddHandler method?
jiiins2Author Commented:
This is what I try to do with maFlag(0), attaching ContextMenuStrip1_ItemClicked. The compiler complains that it can't convert EventArgs to ToolStripMenuClickedItemEventArgs (or similar)...

Of course it would be great to write a generic function, but how can I get access to the properties I indicated?

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

does it work if you just change ToolStripMenuClickedItemEventArgs to EventArgs?
jiiins2Author Commented:
how can I change it?
jiiins2Author Commented:
Done and it seems to work. The problem is that I don't know how to find out which item was clicked... as e.clickeditem doesn't exist anymore.

Any ideas?
jiiins2Author Commented:
I got it, thanks :)
jiiins2Author Commented:
If someone needs it, here is what I did: I created a new sub CMS_ItemClicked that handles the click event of the submenuitems:

item1 = New ToolStripMenuItem("Add File" , Nothing, AddressOf CMS_ItemClicked, "add")

the sender of that sub is the clicked item. So will do the trick.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.