[英]How to get notified when an Item is added to a ToolStripMenuItem.DropDownItems collection
I am developing a MDI application in c#. 我正在用C#开发MDI应用程序。 I am having trouble when two MenuStrip are combined: 将两个MenuStrip合并时遇到麻烦:
I have the parent Parent Form
with its MenuStrip parentMenu
and some ToolStripMenuItem
elements: Option1
, Option2
. 我父父Form
与它MenuStrip parentMenu
和一些ToolStripMenuItem
元素: Option1
, Option2
。
Then I have a child Form
with its MenuStrip parentMenu
and two ToolStripMenuItem
elements: Option1
and OtherOption
. 然后,我有一个孩子Form
与它MenuStrip parentMenu
和两个ToolStripMenuItem
元素: Option1
和OtherOption
。 The merging propierties of these are match-only for Opcion1
and append for OtherOption
这些合并propierties正赛,只为Opcion1
和追加的OtherOption
Now, here is the problem: When the child form is showed and the two MenuStrip
are combined, the event parentMenu.ItemAdded is tiggrered because OtherOption
is added to the parentMenu.Items
collection. 现在,这是问题所在:当显示子窗体并将两个MenuStrip
合并时,由于将OtherOption
添加到parentMenu.Items
集合中,因此将事件parentMenu.ItemAdded设置为tiggrered。 This is not the case for Option1
. Option1
并非如此。 Notice that there may be some elements in Option1.DropDownItems
(in the child form) that are not present in the Option1.DropDownItems
in the parent form. 请注意, Option1.DropDownItems
中的某些元素(在子窗体中)可能不存在于父窗体的Option1.DropDownItems
中。
How can I get notified about these items that being added? 如何获得有关添加的这些项目的通知?
(Just for clarity: Suppose that parentMenu.Items
only contains a ToolStripMenuItem Option1
which only contains a ToolStripMenuItem
, say 'optionA'. Suppose that childMenu.Items
only contains a ToolStripMenuItem Option1
which only contains ToolStripMenuItem optionB
. When the two menus merge, how can I get notified about the item optionB being added the the father? Or how can I get notified about two menus merging, knowing which elements are new?). (只是为了清晰:假设parentMenu.Items
只包含ToolStripMenuItem Option1
只包含一个ToolStripMenuItem
,说“optionA”假设。 childMenu.Items
只包含一个ToolStripMenuItem Option1
只包含ToolStripMenuItem optionB
当两个菜单合并,怎么可以。我收到有关父亲添加的optionB项的通知;或者如何知道两个元素是新的又如何合并两个菜单的通知?)。
"MenuStrip merging moves menu items from one ToolStrip to another rather than cloning them, as was the case with MainMenu." “ MenuStrip合并将菜单项从一个ToolStrip移至另一个,而不是像MainMenu那样将其克隆。 See here . 看这里 。
Because it is moving them rather than cloning them, try the OwnerChanged
event on the ToolStripItem
. 因为它是在移动而不是克隆它们,所以请尝试对ToolStripItem
进行OwnerChanged
事件。 Hook up to this event for each of your items and it will notify you when it is moved to another toolstrip. 为您的每个项目挂起此事件,当它移动到另一个工具栏时,它将通知您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.