Link to home
Start Free TrialLog in
Avatar of jiiins2
jiiins2

asked on

Trouble with contextmenustrip

Hi,

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,
Jay
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")
menuArray(3).DropDownItems.AddRange(maFlag)
 
Me.ContextMenuStrip1.Items.AddRange(menuArray)

Open in new window

Avatar of cdaly33
cdaly33
Flag of United States of America image

What about creating a generic function to handle the click event and attach the event using AddHandler method?

http://msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu.addhandler.aspx
Avatar of jiiins2
jiiins2

ASKER

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?

Thanks!
does it work if you just change ToolStripMenuClickedItemEventArgs to EventArgs?
Avatar of jiiins2

ASKER

how can I change it?
ASKER CERTIFIED SOLUTION
Avatar of cdaly33
cdaly33
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jiiins2

ASKER

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?
Avatar of jiiins2

ASKER

I got it, thanks :)
Avatar of jiiins2

ASKER

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 sender.name will do the trick.