Merge menu strip items for MDI windows
Asked Answered
S

1

5

How can I merge menu items of parent form and child form with same menu name?

Sower answered 18/8, 2010 at 10:35 Comment(0)
E
13

Set the MergeAction of the menu items to "MatchOnly".

Added

Because this can get a little tricky, I'll add a list of steps to make a simple example.

  • Create a new Windows Forms Application.
  • Add a new Windows Form and leave its name Form2.
  • Open Form1 designer (if not already open).
  • Click on the form and set Form1's IsMdiContainer to True.
  • Open the toolbox and add a MenuStrip to Form1.
  • In the "Type Here" box type &File
  • In the sub-item "Type Here" box type A
  • In the sub-item "Type Here" box type B
  • Your MDI container (Form1) should have a File menu with items A and B.
  • Double-click the A item to add a click handler.
  • Add new Form2 { MdiParent = this }.Show(); to the handler method.
  • Open Form2 designer.
  • Open the toolbox and add a MenuStrip to Form2.
  • Note: See below for information about the Visible property on the Form2 MenuStrip, which could be set to False at this point.
  • In the "Type Here" box type &File
  • In the sub-item "Type Here" box type C
  • Your MDI child (Form2) should have a File menu with item C.
  • Click on the File menu item and in the Properties window set MergeAction to MatchOnly.
  • Run the program.

Notice that the File menu items are A and B.

Click File -> A to create a child window.

Notice that the File menu on the container now contains A, B, and C.

Notice also that the File menu on the child is there, but has no items. This is because C was merged.

You can now set the child's MenuStrip.Visible property to False so that the child does not display a menu. It is handy to leave this as True when designing your menus so that you can verify that all the child menu items have been merged correctly (they will be gone from the child menu).

You can use the MergeIndex property to control how child items get merged into the container.

Embellish answered 18/8, 2010 at 14:25 Comment(3)
I tried these exact steps (followed to the letter) on Visual Studio 2013, and they didn't work. I see 2 file menus, one on the MDI container, and one on the MDI child.Gluttonize
I missed the part where you also need to set the visible property of the menu strip control on Form2 to False because that wasn't included in the list of steps, but rather in the paragraph at the end.Gluttonize
I found that if you add the menus later, you can get into a state where you can't get them to merge. I tried several walk-thoughs on my existing form and never got it working. When I created a new parent form and put the menu on first and got it merging properly, I finally got this working.Poddy

© 2022 - 2024 — McMap. All rights reserved.