开发者

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)

开发者 https://www.devze.com 2023-02-19 06:32 出处:网络
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.

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 somewher开发者_高级运维e 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.

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:

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. You can add the child's items to the MenuStrip in the subclass designer and edit it in the normal way. 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消