Merging Menus MDI Applications

152

4.4.3 Merging Menus

Often, the items that should appear on an MDI applications main menu are dependent on the type of document being displayed or on whether any document is displayed at all. Of course, this effect could be achieved in code by dynamically adding and removing menu items each time a child window is activated. However, the Windows Forms framework provides an easier way. If an MDI child form has a main menu of its own, it and the MDI parent forms main menu are merged to produce the menu that is shown to the user when the child form is displayed. Two properties of the MenuItem class affect how the menu items are merged. First, the MergeOrder property determines the order in which the menu items are displayed. This property can be set to any Integer value, and the values dont have to be contiguous. The menu items from the two menus are sorted on this value to determine the order in which the menu items are displayed on screen. For example, consider an MDI parent form that has a main menu with three menu items representing File, Window, and Help menus. Further, say that the MergeOrder properties of these menu items are 10, 20, and 30, respectively. Now, if an MDI child form is displayed and its main menu has, for example, an Edit item with a MergeOrder property value of 15, the menu displayed to the user will have four items: File, Edit, Window, and Help, in that order. Ex am ple 4- 8 shows a revised version of Ex am ple 4- 6 that contains the code necessary to create such a menu; lines shown in boldface have been added to define the main menu and its menu items. Example 4-8. An MDI application with merged menus Imports System Imports System.Windows.Forms Public Module AppModule Public Sub Main Application.RunNew MainForm End Sub End Module Public Class MainForm Inherits Form Declare MainForms main menu. Private myMainMenu As MainMenu Declare the Window menu. Protected WithEvents mnuFile As MenuItem Protected WithEvents mnuWindow As MenuItem Protected WithEvents mnuHelp As MenuItem Public Sub New Set the main window caption. Text = My MDI Application Set this to be an MDI parent form. IsMdiContainer = True Create main menu MyMainMenu = New MainMenu Define menu items mnuFile = New MenuItem mnuWindow = New MenuItem mnuHelp = New MenuItem Set menu properties mnuFile.Text = File mnuFile.MergeOrder = 10 mnuWindow.Text = Window mnuWindow.MergeOrder = 20 153 mnuWindow.MdiList = True mnuHelp.Text = Help mnuHelp.MergeOrder = 30 Add items to menu MyMainMenu.MenuItems.AddmnuFile MyMainMenu.MenuItems.AddmnuWindow MyMainMenu.MenuItems.AddmnuHelp Assign menu to form Me.Menu = MyMainMenu Create a child form. Dim myChild As New DocumentFormMy Document, Me myChild.Show End Sub End Class Public Class DocumentForm Inherits Form Declare menu Private mdiMenu As New MainMenu Declare menu items Protected WithEvents mnuEdit As MenuItem Public Sub NewByVal name As String, ByVal parent As Form Set the document window caption. Text = name Set this to be an MDI child form. MdiParent = parent Instantiate menu and menu items mdiMenu = New MainMenu mnuEdit = New MenuItem Set menu properties mnuEdit.Text = Edit mnuEdit.MergeOrder = 15 Add item to main menu mdiMenu.MenuItems.AddmnuEdit Add menu to child window Me.Menu = mdiMenu End Sub End Class If a menu item in the MDI child form menu has the same MergeOrder value as a menu item in the MDI parent form menu, a second property comes into play. The MergeType property of both MenuItem objects is examined, and the behavior is determined by the combination of their values. The MergeType property is of type MenuMerge an enumeration defined in the System.Windows.Forms namespace and can have one of the following values: Add The menu item appears as a separate item in the target menu, regardless of the setting of the other menu items MergeType property. MergeItems If the other menu items MergeType property is also set to MergeItems , the two menu items are merged into a single item in the target menu. Merging is then recursively applied to the subitems of the source menus, using their MergeOrder and MergeType properties. 154 If the other menu items MergeType property is set to Add , both menu items appear in the target menu just as though both had specified Add . If the other menu items MergeType property is set to Remove , only this menu item appears in the target menu again, the same as specifying Add for this menu item. If the other menu items MergeType property is set to Replace , only the child forms menu item is displayed, regardless of which one is set to MergeItems and which one is set to Replace . This seems like inconsistent behavior and may be a bug. Remove The menu item isnt shown in the target menu, regardless of the setting of the other menu items MergeType property. Replace If the other menu items MergeType property is set to Add , both menu items appear in the target menu just as though both had specified Add . If the other menu items MergeType property is set to MergeItems or Replace , only the child forms menu item is shown. This seems like inconsistent behavior and may be a bug. If the other menu items MergeType property is also set to Replace , only the child forms menu item is shown.

4.4.4 Detecting MDI Child Window Activation