16

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 has that property. I have a ToolStripDropDownButton that has a couple of ToolStripMenuItems and I'd like to be able to pinpoint the parent of those programatically.

user
  • 16,429
  • 28
  • 80
  • 97
  • possible duplicate of [C# - Convert MenuStrip code to ToolStrip](http://stackoverflow.com/questions/4572381/c-convert-menustrip-code-to-toolstrip) – Hans Passant Jan 01 '11 at 00:30
  • There is an [`Owner`](http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.owner.aspx), [`OwnerItem`](http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.owneritem.aspx), and [`Parent`](http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.parent.aspx) property exposed by the [`ToolStripMenuItem` class](http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripmenuitem.aspx). Do *none* of these work for you? It's difficult to understand what you're trying to accomplish and why it isn't working. – Cody Gray - on strike Jan 01 '11 at 02:14
  • @Hans, it isn`t a duplicate but rather a more precise question to get to the root of the problem. And that question has gotten me nowhere, even you tried to answer it with no success and then deleted your answer. – user Jan 01 '11 at 17:17
  • I agree, I don't think this question is an exact duplicate. That's why I didn't vote to close it. I don't think the other really gets to the heart of the problem you're experiencing, and that's why it's been both difficult for people to answer and for you to find a solution. My question is still the same as the one I posted as a comment above—have you tried each of those 3 properties and found that *none* of them work for you? I can't imagine you can't make any of them do what you want. Can you update your question clarifying the problems that you've encountered? – Cody Gray - on strike Jan 02 '11 at 04:06
  • If you construct them as a single menu structure all the way down (i.e. you add to the DropDownItems properties), OwnerItem is the correct way to find the "parent" and you can "walk" the hierarchy back to the top-level MenuStrip/ContextMenu. However, if you create a number of ContextMenuStrip's and then set DropDown properties to these strips, you can theoretically re-use the same strip in multiple places...so there can be multiple paths to get to the same item. :( – AndrewD Oct 25 '11 at 04:09

5 Answers5

25

Try the OwnerItem property.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • 7
    As commented against the question...this only works for menus constructed as a single unit. Setting the DropDown property to another ContextMenu "breaks" the hierarchy i.e. you can only walk up to the set ContextMenu. – AndrewD Oct 25 '11 at 04:11
  • When I created a ContextMenuStrip and added some ToolStripMenuItem items to it, I found that in handling the Click event for an item, the OwnerItem property was null, but the Owner property correctly referenced the ContextMenuStrip. So, pay attention to which works in your particular application. – Brad Oestreicher Feb 06 '19 at 20:57
  • It works perfectly for a generated main menu bar item.. thx for sharing ! – Goodies Mar 17 '20 at 23:44
6

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;
    }
}
rdongart
  • 390
  • 3
  • 10
4

Try this.....

ToolStripMenuItem t = (ToolStripMenuItem)sender;
ContextMenuStrip s = (ContextMenuStrip)t.Owner;
MessageBox.Show(s.SourceControl.Name);
Rikin Patel
  • 8,848
  • 7
  • 70
  • 78
0

After searching many post to this question, I found that this worked for me:

ToolStripMenuItem mi = (ToolStripMenuItem)sender;
ToolStripMenuItem miOwnerItem = (ToolStripMenuItem)(mi.GetCurrentParent() as ToolStripDropDown).OwnerItem;
LarsTech
  • 80,625
  • 14
  • 153
  • 225
mtm
  • 1
0

Here is what you looking for

private void ContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
    contextMenuStrip1.Tag = ((ContextMenuStrip)sender).OwnerItem;
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
    ToolStripMenuItem senderItem = (ToolStripMenuItem)sender;
    var ownerItem = (ToolStripMenuItem)((ContextMenuStrip)senderItem.Owner).Tag;
}
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
xmen
  • 1,947
  • 2
  • 25
  • 47