2

I am trying to convert code that was originally meant for a MenuStrip to something that will work with a ToolStrip. In short, a MenuItem is passed on to Init() and I need to determine what the parent is of the menu item in question. This snippet works fine with a MenuStrip, but I can't seem to get it working with a ToolStrip where the parent is a ToolStripDropDownButton.

Original Code Snippet (Ideal for MenuStrip):

private MenuItem menuItemMRU;
private MenuItem menuItemParent;

public void Init(MenuItem mruItem)
{
    menuItemMRU = mruItem;
    menuItemParent = (MenuItem) menuItemMRU.Parent;
}

This is what I've got so far

private ToolSTripMenuItem menuItemMRU;
private ToolStripDropDownButton menuItemParent;

public void Init(ToolStripMenuItem mruItem)
{
    menuItemMRU = mruItem;
    menuItemParent = (ToolStripMenuItem)menuItemMRU.Owner;
}

This gives me the following error:

Cannot convert type 'System.Windows.Forms.ToolStrip' to 'System.Windows.Forms.ToolStripMenuItem'

user
  • 16,429
  • 28
  • 80
  • 97
  • And what do you mean by `I can't seem to get it working with a ToolStrip`? An error/exception or some other behavior? – decyclone Dec 31 '10 at 21:29
  • Updated question with what I'm currently attempting to use. – user Dec 31 '10 at 21:34
  • You get the error `Cannot convert type 'System.Windows.Forms.ToolStrip' to 'System.Windows.Forms.ToolStripMenuItem'`, because the `Owner` is type of `ToolStrip` and not `ToolStripMenuItem`. – decyclone Dec 31 '10 at 21:38
  • Alright, that makes sense. So how should I go about finding the parent of a ToolStripMenuItem since it doesn't have a "Parent" property like standard MenuItem does. – user Dec 31 '10 at 21:43

1 Answers1

1

The ToolStripItem does however have an OwnerItem property. See msdn

Jester
  • 56,577
  • 4
  • 81
  • 125