Skip to content

Latest commit

 

History

History
98 lines (71 loc) · 5.26 KB

how-to-remove-a-toolstripmenuitem-from-an-mdi-drop-down-menu-windows-forms.md

File metadata and controls

98 lines (71 loc) · 5.26 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Remove a ToolStripMenuItem from an MDI Drop-Down Menu
Learn about how to remove a ToolStripMenuItem from an MDI drop-down menu in Windows Forms via a 12-step procedure.
03/30/2017
csharp
vb
menu items [Windows Forms], removing from MDI drop-down menus
MenuStrip control [Windows Forms], merging
MenuStrip control [Windows Forms], removing
MDI [Windows Forms], merging menu items
bdafe60d-82ee-45bc-97fe-eeefca6e54c1

How to: Remove a ToolStripMenuItem from 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 remove a menu item from the drop-down part of the MDI parent menu. Closing the MDI child window restores the removed menu items to the MDI parent menu.

To remove a MenuStrip from an MDI drop-down menu

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

  4. Add three submenu items to the &File menu item and set their xref:System.Windows.Forms.ToolStripItem.Text%2A properties to &Open, &Import from, and E&xit.

  5. Add two submenu items to the &Import from submenu item and set their xref:System.Windows.Forms.ToolStripItem.Text%2A properties to &Word and &Excel.

  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.ToolStripItem.Text%2A property to &File.

  8. Add an &Import from submenu item to the &File menu of Form2, and add an &Word submenu item to the &File menu.

  9. Set the xref:System.Windows.Forms.MergeAction and xref:System.Windows.Forms.ToolStripItem.MergeIndex%2A properties of the Form2 menu items as shown in the following table.

    Form2 menu item MergeAction value MergeIndex value
    File MatchOnly -1
    Import from MatchOnly -1
    Word Remove -1
  10. In Form1, create an event handler for the xref:System.Windows.Forms.Control.Click event of the &Openxref: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:

  • Two xref:System.Windows.Forms.Form controls named Form1 and Form2.

  • A xref:System.Windows.Forms.MenuStrip control on Form1 named menuStrip1, and a xref:System.Windows.Forms.MenuStrip control on Form2 named menuStrip2.

  • References to the xref:System?displayProperty=nameWithType and xref:System.Windows.Forms?displayProperty=nameWithType assemblies.

See also