Solved

Trouble with contextmenustrip

Posted on 2009-05-17
8
709 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
 

Author Comment

by:jiiins2
ID: 24408492
how can I change it?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now