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_clickedi tem 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.SourceCo ntrol and ContextMenuStrip1.SourceCo ntrol.Find Form() and the clicked item name.
What is the easiest way to achieve this?
Many thanks in advance,
Jay
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_clickedi
In the event, I need access to ContextMenuStrip1.SourceCo
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)
ASKER
This is what I try to do with maFlag(0), attaching ContextMenuStrip1_ItemClic ked. The compiler complains that it can't convert EventArgs to ToolStripMenuClickedItemEv entArgs (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!
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 ToolStripMenuClickedItemEv entArgs to EventArgs?
ASKER
how can I change it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
Any ideas?
ASKER
I got it, thanks :)
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.
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.
http://msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu.addhandler.aspx