Search code examples
c#winformsmditoolstrip

Can I show the ToolStrip of a child Form in the MDIParent Form?


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

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

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


Solution

  • 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.

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

    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.

    Assign distinct names to the child Forms ToolStrip controls.

    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();
    

    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;
    

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

    The reason is explained here:
    How to avoid screen bouncing when adding a new MDI Child Window

    Further readings (MSDN Docs):

    How to: Set Up Automatic Menu Merging for MDI Applications
    Merging Menu Items in the Windows Forms MenuStrip Control