Creating a Window Menu

149 Figure 4-7. A minimal MDI application the output of the code in Ex a m ple 4 - 6 The Form class has two read-only properties related to MDI behavior. The IsMdiChild property returns a Boolean value that indicates whether the form is an MDI child. The MdiChildren property of a parent form contains a collection of references to the forms child forms. The IsMdiChild and MdiChildren properties are both automatically maintained in response to setting the child forms MdiParent properties.

4.4.2 Creating a Window Menu

MDI applications usually have a main-menu item called Window. On this menu appear standard items for cascading, tiling, and activating child windows and arranging the icons of minimized child windows. Figur e 4- 8 shows a typical example. Figure 4-8. A typical Window menu Such a menu is easy to create using the support in Windows Forms. Assuming that you were to do it programmatically, Ex am ple 4- 7 shows a revised version of Ex am ple 4- 6 that has been modified to include a Window menu; the added code is shown in boldface. For details on how to work with menus from the Visual Studio IDE, as well as programmatically, see Sect ion 5.5 in Chapt er 5 . Example 4-7. An MDI application with a Window menu Imports System Imports System.Windows.Forms Public Module AppModule Public Sub Main Application.RunNew MainForm 150 End Sub End Module Public Class MainForm Inherits Form Declare MainForms main menu Private myMainMenu As MainMenu Declare Windows menu Protected WithEvents mnuWindow As MenuItem Protected WithEvents mnuTileHoriz As MenuItem Protected WithEvents mnuCascade As MenuItem Protected WithEvents mnuTileVert As MenuItem Protected WithEvents mnuArrangeAll 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 mnuWindow = New MenuItem mnuTileHoriz = New MenuItem mnuTileVert = New MenuItem mnuCascade = New MenuItem mnuArrangeAll = New MenuItem Set menu properties mnuWindow.Text = Window mnuWindow.MdiList = True mnuTileHoriz.Text = Tile Horizontally mnuTileVert.Text = Tile Vertically mnuCascade.Text = Cascade mnuArrangeAll.Text = Arrange Icons Add items to menu MyMainMenu.MenuItems.AddmnuWindow mnuWindow.MenuItems.AddmnuCascade mnuWindow.MenuItems.AddmnuTileHoriz mnuWindow.MenuItems.AddmnuTileVert mnuWindow.MenuItems.AddmnuArrangeAll Assign menu to form Me.Menu = MyMainMenu Create a child form. Dim myChild As New DocumentFormMy Document, Me myChild.Show End Sub Public Sub mnuCascade_Clicko As Object, e As EventArgs _ Handles mnuCascade.Click LayoutMdiMdiLayout.Cascade End Sub Public Sub mnuTileHoriz_Clicko As Object, e As EventArgs _ Handles mnuTileHoriz.Click LayoutMdiMdiLayout.TileHorizontal End Sub Public Sub mnuTileVert_Clicko As Object, e As EventArgs _ Handles mnuTileVert.Click 151 LayoutMdiMdiLayout.TileVertical End Sub Public Sub mnuArrangeAll_Clicko As Object, e As EventArgs _ Handles mnuArrangeAll.Click LayoutMdiMdiLayout.ArrangeIcons End Sub End Class Public Class DocumentForm Inherits Form 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 End Sub End Class To add a Window menu to the parent form of an MDI application, perform the following steps. First, add a menu item to the MDI parent forms main menu, setting its Text property to anything desired usually Window and its MdiList property to True . It is the MdiList property that makes the Window menu a Window menu. Setting the MdiList property to True causes the Windows Forms framework to add and delete menu items to and from this menu item as necessary. This in turn will always display the current list of MDI child windows in the menu. Next, add menu items for Cascade, Tile Horizontally, Tile Vertically, and Arrange Icons. In the Click event handler for each of these menu items, call the Form classs LayoutMdi method, passing the appropriate parameter value for the desired action. The syntax of the LayoutMdi method is: Public Sub LayoutMdiByVal value As MdiLayout The methods single argument must be a value from the MdiLayout enumeration defined in the System.Windows.Forms namespace. The values in this enumeration are: ArrangeIcons Indicates that the icons for the minimized MDI child windows should be neatly arranged. Cascade Indicates that the MDI child windows should be cascaded displayed overlapping each other. TileHorizontal Indicates that the MDI child windows should be tiled displayed without overlapping, with each child window filling the width of the MDI parent. TileVertical Indicates that the MDI child windows should be tiled, with each child window filling the height of the MDI parent. 152

4.4.3 Merging Menus