MDI, MenuStrips and Windows Forms 2.0

Working on an MDI app.  I wanted to have the main menu change based on the child window that's active.  Windows Forms provides a way to do this via menu merging.


Menu merging underwent some changes from WinForms 1.1 to WinForms 2.0.  A few tips:


  • set MergeIndex of child menu item to desired index in parent (0 is first index)
  • for each child menu item
  •   if something is already at the desired index then use merge action
  •      if merge action is insert then insert the child menu item above the item already there
  •      if merge action is append, put it after
  •      ...
  •    consider next child in light of new ordering

The last point bears explanation.  If the merge action is insert then the child item pushes down whatever was there.  That increases its new index.  So if you want the next child item to appear below the first child item in the merged menu then you will need to set its MergeIndex taking this into account.

e.g., child menu item1 has merge index of 2 and merge action of insert.

To stuff child menu item1 and 2 into the parent menu child menu item2 will need a merge index of 3.  This is the case because, after the first loop child menu item1 pushed the previous occupant into position 3.  So child menu item2 needs to push that occupant (in position 3) down.

No comments:

Post a Comment