Trouble with contextmenustrip

Posted on 2009-05-17
Last Modified: 2012-05-07

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

Question by:jiiins2
  • 5
  • 3

Expert Comment

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

Author Comment

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?


Expert Comment

ID: 24408481
does it work if you just change ToolStripMenuClickedItemEventArgs to EventArgs?

Author Comment

ID: 24408492
how can I change it?
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.


Accepted Solution

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

Author Comment

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?

Author Comment

ID: 24430220
I got it, thanks :)

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

17 Experts available now in Live!

Get 1:1 Help Now