How to get the referece to ContextMenuStrip from its underlying sub menuitem which is a ToolStripMenuItem of a ToolStripDropDownMenu which is a Dropdown of a ToolstripMenuItem of this ContextMenuStrip?
the problem lies in the ToolStripDropDownMenu class,where can't find a property refer to upperlevel objects such as its assciated ToolStripMenuItem.Its Parent,Container properties and GetContainerControl() method all return null.
My purpose is to know the SourceControl of the ContextMenuStrip when any of its underlying menu items are clicked.
You need a backward loop to get the first
OwnerItemproperty of a givenToolStripItem(or one of the derived classes) in a branch then you can get the mainToolStrip(which can be aToolStrip,MenuStrip,StatusStrip, orContextMenuStripcontrol) through theToolStripItem.Ownerproperty.Consider the following extension methods:
Here's an example that shows how to get the
ContextMenuStrip.SourceControlin theItemClickedevent: