C# - 将 MenuStrip 代码转换为 ToolStrip
我正在尝试将最初用于 MenuStrip 的代码转换为可用于 ToolStrip 的代码。简而言之,MenuItem 被传递给 Init(),我需要确定相关菜单项的父项是什么。此代码片段可以与 MenuStrip 配合使用,但我似乎无法将其与父级为 ToolStripDropDownButton 的 ToolStrip 配合使用。
原始代码片段(MenuStrip的理想选择):
private MenuItem menuItemMRU;
private MenuItem menuItemParent;
public void Init(MenuItem mruItem)
{
menuItemMRU = mruItem;
menuItemParent = (MenuItem) menuItemMRU.Parent;
}
这是我到目前为止所得到的
private ToolSTripMenuItem menuItemMRU;
private ToolStripDropDownButton menuItemParent;
public void Init(ToolStripMenuItem mruItem)
{
menuItemMRU = mruItem;
menuItemParent = (ToolStripMenuItem)menuItemMRU.Owner;
}
这给了我以下错误:
无法将类型“System.Windows.Forms.ToolStrip”转换为“System.Windows.Forms.ToolStripMenuItem”
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
然而,
ToolStripItem
确实有一个OwnerItem
属性。请参阅 msdnThe
ToolStripItem
does however have anOwnerItem
property. See msdn