views:

25

answers:

2

I have single ContextMenuStrip attached to more controls.

In use the Opening event of ContextMenuStrip to filter/disable some context entries. In this case the property ContexteMenuStrip.SourceControl is set correctly.

The problem I have is on the Click event of a ToolStripMenuItem. This item is inside a ToolStripDropDown.

I get the parent item with code:

Dim tsmi As ToolStripMenuItem = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ToolStripDropDown).OwnerItem, ToolStripMenuItem)

then I get the ContextMenuStrip:

Dim contextMenu As ContextMenuStrip = DirectCast(tsmi.Owner, ContextMenuStrip)

but now, if I check contextMenu.SourceControl is Nothing.

Do you have any idea what is wrong or why SourceControl is not set in this case?

Thank you in advance

A: 

I had this exact same question a couple weeks ago, and in reality, nobody could figure out why this behavior was occurring. Take a look at the question I asked, the solution I was given worked great.

AndyPerfect
@AndyPerfect thank you, but I need to get the SourceControl linked to the ContextMenu when the item is clicked because this menu is reused on more DataGridView instances. I don't like to create a different ContextMenu for each DataGridView.
marco.ragogna
There's no need to have a different ContextMenu for each form object. Rather, have a single ContextMenu. As the form is loading, add a Mouse.click handler for each object, which calls a method which in turn opens up the ContextMenu. You should be able to handle it perfectly like that. If you really need the source control, which in reality was the form object clicked, have a global variable such as "ObjectToOpenContextMenu" that is set withing that right click handler. That way, depending on what the user clicks within the contextmenu, the global variable is, in effect, the sourcecontrol.
AndyPerfect
A: 

I found a workaround that is not really the answer to the question. So I will leave it open for a while.

I used the ContextMenuStrip Opening event to store locally the source object.

Private Sub contextGrid_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles contextGrid.Opening

  _ContextSourceGrid = DirectCast(contextGrid.SourceControl, DataGridView)

End Sub

and refer directly to the saved object inside all ToolStripMenuItem Click events.

marco.ragogna