Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-append-a-menustrip-to-an-mdi-parent-window-windows-forms.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

4.6 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Append a MenuStrip to an MDI Parent Window 03/30/2017
csharp
vb
MenuStrip control [Windows Forms], merging
MenuStrip control [Windows Forms], appending
MDI [Windows Forms], merging menu items
ab70c936-b452-4653-b417-17be57bb795b

How to: Append a MenuStrip to an MDI Parent Window (Windows Forms)

In some applications, the kind of a multiple-document interface (MDI) child window can be different from the MDI parent window. For example, the MDI parent might be a spreadsheet, and the MDI child might be a chart. In that case, you want to update the contents of the MDI parent's menu with the contents of the MDI child's menu as MDI child windows of different kinds are activated.

The following procedure uses the xref:System.Windows.Forms.Form.IsMdiContainer%2A, xref:System.Windows.Forms.ToolStrip.AllowMerge%2A, xref:System.Windows.Forms.MergeAction, and xref:System.Windows.Forms.ToolStripItem.MergeIndex%2A properties to append the MDI child menu to the MDI parent menu. Closing the MDI child window removes the appended menu from the MDI parent.

Also see Multiple-Document Interface (MDI) Applications.

To append a menu item to an MDI parent

  1. Create a form and set its xref:System.Windows.Forms.Form.IsMdiContainer%2A property to true.

  2. Add a xref:System.Windows.Forms.MenuStrip to Form1 and set the xref:System.Windows.Forms.ToolStrip.AllowMerge%2A property of the xref:System.Windows.Forms.MenuStrip to true.

  3. Set the xref:System.Windows.Forms.ToolStripItem.Visible%2A property of the Form1xref:System.Windows.Forms.MenuStrip to false.

  4. Add a top-level menu item to the Form1xref:System.Windows.Forms.MenuStrip and set its xref:System.Windows.Forms.Control.Text%2A property to &File.

  5. Add a submenu item to the &File menu item and set its xref:System.Windows.Forms.Form.Text%2A property to &Open.

  6. Add a form to the project, add a xref:System.Windows.Forms.MenuStrip to the form, and set the xref:System.Windows.Forms.ToolStrip.AllowMerge%2A property of the Form2xref:System.Windows.Forms.MenuStrip to true.

  7. Add a top-level menu item to the Form2xref:System.Windows.Forms.MenuStrip and set its xref:System.Windows.Forms.Form.Text%2A property to &Special.

  8. Add two submenu items to the &Special menu item and set their xref:System.Windows.Forms.Form.Text%2A properties to Command&1 and Command&2, respectively.

  9. Set the xref:System.Windows.Forms.MergeAction property of the &Special, Command&1, and Command&2 menu items to xref:System.Windows.Forms.MergeAction.Append.

  10. Create an event handler for the xref:System.Windows.Forms.Control.Click event of the &Open xref:System.Windows.Forms.ToolStripMenuItem.

  11. Within the event handler, insert code similar to the following code example to create and display new instances of Form2 as MDI children of Form1.

    Private Sub openToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openToolStripMenuItem.Click  
        Dim NewMDIChild As New Form2()  
        'Set the parent form of the child window.  
        NewMDIChild.MdiParent = Me  
        'Display the new form.  
        NewMDIChild.Show()  
    End Sub  
    
    private void openToolStripMenuItem_Click(object sender, EventArgs e)  
    {  
        Form2 newMDIChild = new Form2();  
        // Set the parent form of the child window.  
        newMDIChild.MdiParent = this;  
        // Display the new form.  
        newMDIChild.Show();  
    }  
    
  12. Place code similar to the following code example in the &Openxref:System.Windows.Forms.ToolStripMenuItem to register the event handler.

    Private Sub openToolStripMenuItem_Click(sender As Object, e As _  
    EventArgs) Handles openToolStripMenuItem.Click  
    
    this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);  
    

Compiling the Code

This example requires: