Solved

Trouble with contextmenustrip

Posted on 2009-05-17
8
717 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:jiiins2
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:cdaly33
ID: 24407594
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
0
 

Author Comment

by:jiiins2
ID: 24408456
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!
0
 
LVL 9

Expert Comment

by:cdaly33
ID: 24408481
does it work if you just change ToolStripMenuClickedItemEventArgs to EventArgs?
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:jiiins2
ID: 24408492
how can I change it?
0
 
LVL 9

Accepted Solution

by:
cdaly33 earned 500 total points
ID: 24408526
In the ContextMenuStrip1_ItemClicked change the argument type from ToolStripMenuClickedItemEventArgs to EventArgs.
0
 

Author Comment

by:jiiins2
ID: 24429446
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?
0
 

Author Comment

by:jiiins2
ID: 24430220
I got it, thanks :)
0
 

Author Comment

by:jiiins2
ID: 24430247
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.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question