C# - Convert MenuStrip code to ToolStrip

浪子不回头ぞ 提交于 2020-01-13 20:30:13

问题


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'


回答1:


The ToolStripItem does however have an OwnerItem property. See msdn



来源:https://stackoverflow.com/questions/4572381/c-sharp-convert-menustrip-code-to-toolstrip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!