The MDI related features around MenuStrip are described below.
Using a MenuStrip in MDI requires Form.MainMenuStrip to be set in order to identiy
the controlling MenuStrip. It will be used for child window control menu merging
when children are maximized. Automatic merging between child and parent forms is
triggered by child activation.
ToolStripMenuItem ToolStrip.MDIWindowListItem
This property identifies the item whose drop down should be populated
with the titles of MDI children associated with this MDI Parent.
ToolStripMenuItem.IsMdiWindowListEntry
This property can be used to do post item customization of the
MDI window list. This is how you would identify which items are sourced from an
MDI child.
Automatic merging
Merging in the automatic case is triggered by MDI child activation
and deactivation. Upon activation, the MenuStrip in the child form is merged into
the MDIParent's MainMenuStrip. Later with subsequent deactivation/activation pairs
as a new form becomes active the last form is unmerged (RevertMerge), then the new
form is merged. This behavior can be tweaked via mergeAction property on each ToolStripItem
and through the AllowMerge property on MenuStrip.
Only MenuStrips participate in automatic merging. To merge ToolStrips,
StatusStrip etc, you must merge them manually.
Procedure for automatically merging an MDI child menu into a MDI parent
The following proceedure discusses how to use automatic merging
in an MDI application with MenuStrip.
- Create the MDI parent form as usual, setting MainForm.IsMdiContainer
= true.
- Add a MenuStrip to the MDI parent, setting MainForm.MainMenuStrip
= menuStrip1
- Create an MDI child form, setting MdiChildForm.MdiParent =
MainForm
- Add a MenuStrip to the MDI child
- Set the MenuStrip in the MdiChildForm to Visible = false
- Add menu items to the MdiChildForm that you want to merge
into the MainForm's MainMenuStrip when the MdiChildForm is activated.
- Use the MergeAction property on the items in the MdiChild's
MenuStrip to control how the items on the MDIChildForm merge into the MainForm.
What are the MergeAction values and what do they mean?
When thinking about Merging, it is often convenient to describe
the action in terms of a target and source:
Target - this is the ToolStrip you're merging into (e.g.
a Main MenuStrip on your form)
Source - this is the ToolStrip with items you want to
merge into the Target toolstrip. (e.g. a menu from an MDI child from)
Merge Action
The merge action should be set on items in the "Source"
toolstrip - e.g. your MDI child menu strip.
Append
- (default) adds the Source item to the end of the
Target Items collection
Insert
- adds the Source item to the Target Items collections as specified
by the MergeIndex property set on the Source Item.
Replace
- finds a match (using Text property, then MergeIndex if no match),
then replaces the matching Target Item with the Source Item.
- (e.g, MDI child item replaces item from MainMenuStrip)
MatchOnly
- finds a match (using Text property, then MergeIndex if no match),
then adds all the Source Item's DropDownItems to the Target Item.
- (e.g. an MDI child wants to add a menu item to MainMenuStrip's
Save As-> menu).
Remove
- finds a match (using Text property, then MergeIndex if no match),
then removes the item from the Target ToolStrip.
- (e.g. MDI child can remove the save menu item from MainMenuStrip).
-
Note the usefulness of MatchOnly - it can be used to build up
a menu structure to insert/add/remove into a submenu. The most frequently used
MergeActions will be MatchOnly, Append, and Insert.