Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trouble with contextmenustrip

Posted on 2009-05-17
8
Medium Priority
?
726 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

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

Accepted Solution

by:
cdaly33 earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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