我可以在 MDIParent 窗体中显示子窗体的 ToolStrip 吗? [英] Can I show the ToolStrip of a child Form in the MDIParent Form?

查看:34
本文介绍了我可以在 MDIParent 窗体中显示子窗体的 ToolStrip 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MDI 表单,我想将子表单的 ToolStrip 与父 Mdi 表单合并/添加.

I have an MDI Form and I want to merge/add the child Form's ToolStrip with parent Mdi Form.

通常,子窗体的 ToolStrip 仅显示在所有者子窗体内,但我想将其与 MdiParent 合并.

Normally, a child Form's ToolStrip is shown only inside the owner child Form, but I want to merge it with the MdiParent's.

我可以用 MenuStrip 做到这一点,但几乎找不到 ToolStrip 来做到这一点.

I can do it with the MenuStrip but hardly find the ToolStrip to do the same.

推荐答案

ToolStripManager.Merge() 方法可用于合并 MdiParent 的 ToolStrip 和子 Form 的 Toolstrip.子窗体的工具条将移动到 MdiParent 的工具条.

The ToolStripManager.Merge() method can be used to merge the MdiParent's ToolStrip and a child Form's Toolstrip. The child Form's ToolStrip will be moved to the MdiParent's ToolStrip.

ToolStripManager.RevertMerge() 方法会在子 Form 关闭时移除子 Form 的 ToolStrip.

The ToolStripManager.RevertMerge() method will remove the child Form's ToolStrip when the child Form is closed.

当然,使用您分配给 MdiParent ToolStrip(此处为 MdiToolStrip)和子 Form ToolStrip(此处命名为 toolStrip1)的真实名称并调整 Forms使用的名称.

Of course, use the real names you have assigned to MdiParent ToolStrip (here, MdiToolStrip) and the child Form ToolStrip (here, named toolStrip1) and adapt the Forms names to what you're using.

为子 Forms ToolStrip 控件分配不同的名称.

Assign distinct names to the child Forms ToolStrip controls.

创建子 Form 实例时,在显示之前添加:

When you create a child Form instance, add this before showing it:

var child1 = new ChildForm1();
child1.MdiParent = this;
ToolStripManager.Merge(child1.toolStrip1, this.MdiToolStrip);
child1.toolStrip1.Visible = false;
child1.FormClosed += (s, ev) => { ToolStripManager.RevertMerge(this.MdiToolStrip, child1.toolStrip1); };
child1.Show();

如果您在 MdiParent 和子 Forms 中有 MenuStrip,ToolStripMenusItems 将自动合并,但我建议您在 MdiParent 构造函数中的 InitializeComponent() 之后添加它:

If you have a MenuStrip in the MdiParent and in your child Forms, the ToolStripMenusItems will be merged automatically, but I suggest you add this right after InitializeComponent() in the MdiParent Constructor:

this.MainMenuStrip = MdiMenuStrip;

其中 MdiMenuStrip 是分配给 MdiParent 的 MenuStrip 的名称.

where MdiMenuStrip is the name assigned to the MdiParent's MenuStrip.

原因解释如下:
添加新 MDI 子窗口时如何避免屏幕弹跳

进一步阅读(MSDN 文档):

Further readings (MSDN Docs):

如何:为 MDI 应用程序设置自动菜单合并
合并 Windows 窗体 MenuStrip 控件中的菜单项

这篇关于我可以在 MDIParent 窗体中显示子窗体的 ToolStrip 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆