| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Insert a MenuStrip into an MDI Drop-Down Menu (Windows Forms) |
03/30/2017 |
.net-framework |
|
article |
|
|
0fad444e-26d9-49af-8860-044d9c10d608 |
14 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Insert a MenuStrip into an MDI Drop-Down Menu (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 insert a group of menu items from the MDI child menu into the drop-down part of the MDI parent menu. Closing the MDI child window removes the inserted menu items from the MDI parent.
To insert a MenuStrip into an MDI drop-down menu
-
Create a form and set its xref:System.Windows.Forms.Form.IsMdiContainer%2A property to
true. -
Add a xref:System.Windows.Forms.MenuStrip to
Form1and set the xref:System.Windows.Forms.ToolStrip.AllowMerge%2A property of the xref:System.Windows.Forms.MenuStrip totrue. -
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. -
Add three submenu items to the
&Filemenu item and set their xref:System.Windows.Forms.ToolStripItem.Text%2A properties to&Open,&Import from, andE&xit. -
Add two submenu items to the
&Import fromsubmenu item and set their xref:System.Windows.Forms.ToolStripItem.Text%2A properties to&Wordand&Excel. -
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 totrue. -
Add a top-level menu item to the
Form2xref:System.Windows.Forms.MenuStrip and set its xref:System.Windows.Forms.ToolStripItem.Text%2A property to&File. -
Add submenu items to the
&Filemenu ofForm2in the following order: a xref:System.Windows.Forms.ToolStripSeparator,&Save,&Close``and Save, and another xref:System.Windows.Forms.ToolStripSeparator. -
Set the xref:System.Windows.Forms.MergeAction and xref:System.Windows.Forms.ToolStripItem.MergeIndex%2A properties of the
Form2menu items as shown in the following table.Form2 menu item MergeAction value MergeIndex value File MatchOnly -1 Separator Insert 2 Save Insert 3 Save and Close Insert 4 Separator Insert 5 -
Create an event handler for the xref:System.Windows.Forms.Control.Click event of the
&Openxref:System.Windows.Forms.ToolStripMenuItem. -
Within the event handler, insert code similar to the following code example to create and display new instances of
Form2as 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
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
&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:
-
Two xref:System.Windows.Forms.Form controls named
Form1andForm2. -
A xref:System.Windows.Forms.MenuStrip control on
Form1namedmenuStrip1, and a xref:System.Windows.Forms.MenuStrip control onForm2namedmenuStrip2. -
References to the xref:System?displayProperty=nameWithType and xref:System.Windows.Forms?displayProperty=nameWithType assemblies.
See Also
How to: Create MDI Parent Forms
How to: Create MDI Child Forms
MenuStrip Control Overview