2010-03-16 44 views

回答

1
`ToolStripItem.OwnerItem` [http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.owneritem.aspx][1]

即屬性可以用來將菜單項的樹向上移動到最高級別的項目......並不是您在菜單的Owner屬性之後?

var control = ((ContextMenuStrip)topLevelMenuItem.Owner).SourceControl; 

顯然使用as等,做你的null檢查...

如果我錯過了現場後也許通過代碼內置菜單的代碼片段,以澄清類型(文本菜單VS等的ContextMenuStrip )

PK :-)

+0

你甚至嘗試過嗎?用'as'語句將'OwnerItem'投射到'ContextMenu'甚至不能編譯(即:'ContextMenu'不能*爲'ToolStripItem')。 – 2010-03-17 12:04:48

+0

這是菜單項的父級。 – 2010-03-17 20:01:05

+0

查看更新的答案... – 2010-03-18 21:05:08

0

我需要使用Owner屬性作爲ContextMenuStrip

ToolStripDropDownItem t = sender as ToolStripDropDownItem; 
if (t == null) 
    return null; 
ContextMenuStrip cm = t.Owner as ContextMenuStrip; 
if (cm == null) 
    return null; 
return cm.SourceControl;