Search code examples
c#contextmenustripownership

get contextmenustrip from toolstripmenuitem


Do I use the Owner, OwnerItem, or the Container property?

What if the ToolStripMenuItem is a member of another tsmi, which in turn is a member of another tsmi, etc.?

The purpose of this is to be able to add an event handler that gets at the TreeView that has the ContextMenuStrip that has that ToolStripMenuItem.

Abstracting a bit, is there a general way to get at the owners of controls, until you reach the one you're interested in? Just cast and hope?


Solution

  • use ToolStripMenuItem1.GetCurrentParent()

    for getting parent of controls which inherited from System.Windows.Forms.Control read Parent

    like Button1.Parent, ....