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 IsMdiContainer, AllowMerge, MergeAction, and MergeIndex 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.
Multiple-Document Interface (MDI) Applications
Multiple-Document Interface (MDI) Applications
Multiple-Document Interface (MDI) Applications
Multiple-Document Interface (MDI) Applications
To append a menu item to an MDI parent
Create a form and set its IsMdiContainer property to true.
Add a MenuStrip to
Form1
and set the AllowMerge property of the MenuStrip to true.Set the Visible property of the
Form1
MenuStrip to false.Add a top-level menu item to the
Form1
MenuStrip and set its Text property to&File
.Add a submenu item to the
&File
menu item and set its Text property to&Open
.Add a form to the project, add a MenuStrip to the form, and set the AllowMerge property of the
Form2
MenuStrip to true.Add a top-level menu item to the
Form2
MenuStrip and set its Text property to&Special
.Add two submenu items to the
&Special
menu item and set their Text properties toCommand&1
andCommand&2
, respectively.Set the MergeAction property of the
&Special
,Command&1
, andCommand&2
menu items to Append.Create an event handler for the Click event of the
&New
ToolStripMenuItem.Within the event handler, insert code similar to the following code example to create and display new instances of
Form2
as MDI children ofForm1
.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
[C#]
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(); }
Place code similar to the following code example in the
&Open
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:
Two Form controls named
Form1
andForm2
.A MenuStrip control on
Form1
namedmenuStrip1
, and a MenuStrip control onForm2
namedmenuStrip2
.References to the System and System.Windows.Forms assemblies.