2013-11-09 27 views
0

context menu parent?如何連接到父項目子菜單項

我看到了這個問題,並檢查它運行良好。 我編輯了一些並再次測試

  • 菜單1
    • 菜單2
  • 菜單3

當我做出那樣的菜單和測試,有一些問題在菜單2上。

on Menu3

ToolStripMenuItem item = (sender as ToolStripMenuItem); //works well 
ContextMenuStrip owner = item.source; //works well 
string str = owner.SourceControl.Text; //works well 

在菜單2

ToolStripMenuItem item = (sender as ToolStripMenuItem); //works well 
ContextMenuStrip owner = item.OwnerItem.source; //works well 
string str = owner.SourceControl.Text; //error - owner.SourceControl is null 

我測試了這一點,並得到不同的結果

chart1.ContextMenuStrip.SourceControl

在菜單3,顯示良好。 在Menu2上顯示空白。

如何連接到子菜單項的父項?

+0

我似乎記得這是一個衆所周知的錯誤。爲了解決它,在你的類中聲明一個私有變量_chosenContextMenu。然後,您不斷更新其值,以便始終知道父菜單。 – user1646737

+0

像你一樣,我以另一種方式做到了。當鼠標處於控制狀態時,將事件添加到菜單的父級。事件保存發生事件的控制。之後,我可以很好地使用我的功能。謝謝 – Temp

回答

0

如果未使用ToolStripMenuItem的標記,則可以將其父ToolStripMenuItem存儲到其標記中。

相關問題