How can I determine the parent of a ToolStripMenuItem? With a normal MenuStrip all you have to do is use the Parent property, but it doesn\'t seem that ToolStripMenuItem ha
This works for me:
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
ToolStrip toolStrip = menuItem.GetCurrentParent();
...from this, you can devise a method to bring you from a random ToolStripMenuItem to the top-most level such:
public static class ToolStripItemExtension
{
public static ContextMenuStrip GetContextMenuStrip(this ToolStripItem item)
{
ToolStripItem itemCheck = item;
while (!(itemCheck.GetCurrentParent() is ContextMenuStrip) && itemCheck.GetCurrentParent() is ToolStripDropDown)
{
itemCheck = (itemCheck.GetCurrentParent() as ToolStripDropDown).OwnerItem;
}
return itemCheck.GetCurrentParent() as ContextMenuStrip;
}
}