다음을 통해 공유


Merging Menu Items in the Windows Forms MenuStrip Control

If you have a multiple-document interface (MDI) application, you can merge menu items or whole menus from the child form into the menus of the parent form.

This topic describes the basic concepts associated with merging menu items in an MDI application.

General Concepts

Merging procedures involve both a target and a source control:

  • The target is the MenuStrip control on the main or MDI parent form into which you are merging menu items.

  • The source is the MenuStrip control on the MDI child form that contains the menu items you want to merge into the target menu.

The MdiWindowListItem property identifies the menu item whose drop-down list you will populate with the titles of the current MDI parent form's MDI children. For example, you typically list MDI children that are currently open on the Window menu.

The IsMdiWindowListEntry property identifies which menu items come from a MenuStrip on an MDI child form.

You can merge menu items manually or automatically. The menu items merge in the same way for both methods, but the merge is activated differently, as discussed in the "Manual Merging" and "Automatic Merging" sections later in this topic. In both manual and automatic merging, each merge action affects the next merge action.

MenuStrip merging moves menu items from one ToolStrip to another rather than cloning them, as was the case with MainMenu.

MergeAction Values

You set the merge action on menu items in the source MenuStrip using the MergeAction property.

The following table describes the meaning and typical use of the available merge actions.

MergeAction Value Description Typical Use

Append

(Default) Adds the source item to the end of the target item's collection.

Adding menu items to the end of the menu when some part of the program is activated.

Insert

Adds the source item to the target item's collection, in the location specified by the MergeIndex property set on the source item.

Adding menu items to the middle or the beginning of the menu when some part of the program is activated.

If the value of MergeIndex is the same for both menu items, they are added in reverse order. Set MergeIndex appropriately to preserve the original order.

Replace

Finds a text match, or uses the MergeIndex value if no text match is found, and then replaces the matching target menu item with the source menu item.

Replacing a target menu item with a source menu item of the same name that does something different.

MatchOnly

Finds a text match, or uses the MergeIndex value if no text match is found, and then adds all the drop-down items from the source to the target.

Building a menu structure that inserts or adds menu items into a submenu, or removes menu items from a submenu. For example, you can add a menu item from an MDI child to a main MenuStrip Save As menu.

MatchOnly allows you to navigate through the menu structure without taking any action. It provides a way to evaluate the subsequent items.

Remove

Finds a text match, or uses the MergeIndex value if no text match is found, and then removes the item from the target.

Removing a menu item from the target MenuStrip.

Manual Merging

Only MenuStrip controls participate in automatic merging. To combine the items of other controls, such as ToolStrip and StatusStrip controls, you must merge them manually, by calling the Merge and RevertMerge methods in your code as required.

Automatic Merging

You can use automatic merging for MDI applications by activating the source form. To use a MenuStrip in an MDI application, set the MainMenuStrip property to the target MenuStrip so that merging actions performed on the source MenuStrip are reflected in the target MenuStrip.

You can trigger automatic merging by activating the MenuStrip on the MDI source. Upon activation, the source MenuStrip is merged into the MDI target. When a new form becomes active, the merge is reverted on the last form and triggered on the new form. You can control this behavior by setting the MergeAction property as needed on each ToolStripItem, and by setting the AllowMerge property on each MenuStrip.

See Also

Tasks

How to: Create an MDI Window List with MenuStrip (Windows Forms)
How to: Set Up Automatic Menu Merging for MDI Applications

Reference

ToolStripManager
MenuStrip

Other Resources

MenuStrip Control (Windows Forms)