简体   繁体   English

将项目添加到ToolStripMenuItem.DropDownItems集合时如何获得通知

[英]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元素: Option1Option2

Then I have a child Form with its MenuStrip parentMenu and two ToolStripMenuItem elements: Option1 and OtherOption . 然后,我有一个孩子Form与它MenuStrip parentMenu和两个ToolStripMenuItem元素: Option1OtherOption 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM