简体   繁体   English

如何将子窗体类中的菜单条中的其他项合并到其基类中的菜单条中的原始项? (WInforms)

[英]How can I merge additional items in a menustrip in a child form class to the original items in the menustrip in its base class? (WInforms)

I'm making a form that is a subclass of another form, the 'base form' 我正在制作一个表单,该表单是另一种表单(“基本表单”)的子类

In the base form I have a menu strip. 在基本形式中,我有一个菜单栏。 It will have a few items in there. 那里会有一些物品。

But then, in the child form, I want to have a menu strip too except it has to have all the items the base menu strip contains. 但是然后,在子窗体中,我也要有一个菜单栏,除了它必须具有基本菜单栏包含的所有项。 I read somewhere you can merge menustrips... but I want to have designer support too! 我读过某个地方,您可以合并菜单栏...但是我也希望得到设计师的支持!

That is, if I open up the child form in the designer, it will show all the base form items plus the new child form items. 也就是说,如果我在设计器中打开子窗体,它将显示所有基本窗体项目以及新的子窗体项目。 Preferably, I want the base form items to be locked. 最好,我希望基本表单项被锁定。

Is this possible? 这可能吗? If not, I guess I could merge the base menustrip in after InitializeComponent in the child form. 如果没有,我想我可以在子窗体的InitializeComponent之后合并基本菜单栏。

Any ideas? 有任何想法吗?

You can merge two menus like this: 您可以像这样合并两个菜单:

ToolStripManager.Merge(source, target)

for example, in form2.load, you could use this to combine form2's menu with form1's menu: 例如,在form2.load中,您可以使用此命令将form2的菜单与form1的菜单结合起来:

ToolStripManager.Merge(Form1.MenuStrip1, MenuStrip1)

The OP says "I want to have designer support too", which is possible although you still have to use the Merge() call proposed by @xpda. OP表示“我也希望获得设计人员支持”,尽管您仍然必须使用@xpda提出的Merge()调用,这是可能的。 You can add the child's items to the MenuStrip in the subclass designer and edit it in the normal way. 您可以将子项添加到子类设计器的MenuStrip中,然后以常规方式对其进行编辑。 In the designer this strip is shown beneath the base class's menu strip. 在设计器中,该条显示在基类的菜单条下方。

When you execute the form the Merge(source, target) call removes the items from the source (child) MenuStrip and copies them to the target (base) MenuStrip. 当您执行表单时,Merge(源,目标)调用将从源(子级) MenuStrip删除项目,并将其复制到目标(基)MenuStrip中。 However the child menu strip, now blank, is still visible below the main menu. 但是 ,子菜单条(现在为空白)在主菜单下仍然可见。 You have to set its Visible property false, which you can do in the designer or programmatically after the Merge() call. 您必须将其Visible属性设置为false,您可以在设计器中或在调用Merge()之后以编程方式进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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