Creating the Parent Form for the Sample Application

Let’s begin working on the sample application by creating an MDI parent form, which will contain the other forms in the application as well as common menu items:

1.
Start by creating a new Windows Application project. Name the project MDIDemo.

2.
Click Form1.vb in the Solution Explorer window; then change its File Name property in the Properties window to frmParent.vb.

3.
Right-click the form’s Designer window, and then select View Code from the context menu. Rename the form by changing Public Class Form1 at the top of the Code window to Public Class frmParent. (It is possible that the name of the class was changed for you when you changed the File Name property in the previous step.)

4.
Right-click MDIDemo in the Solution Explorer window; then select Properties from the context menu. Change the project’s Startup Object to frmParent, if it is not already set this way.

5.
Change frmParent’s Text property to MDI Text Editor.

6.
View frmParent’s Designer window, and then set its IsMDIContainer property to True. The background of the form will then take on a darker appearance, indicating it is a parent form, as shown in Figure 15.3.

Figure 15.3. The MDI parent acts as a container for the child forms.


7.
Drag an OpenFileDialog control to the form. This will be used when the user wants to open an existing file. Change its Name property to dlgOpen, its DefaultExt property to txt, and its Filter property to Text Files (*.txt)|*.txt|All Files (*.*)|*.*.

Menu Considerations

As you learned in Chapter 10, “Understanding Windows Forms,” you can add a MainMenu control to a form to provide menu functionality. You may be wondering how menus are displayed in an MDI environment, since MDI parent and child forms can each have their own menu systems. Visual Basic .NET provides a series of properties that govern how menus are displayed when both an active child window and its parent window contain menu controls. These properties, as well as the special MDIList property, are discussed in the following paragraphs.

→ For more information on using the MainMenu control, seeUsing the Menu Editor,” p. 264

The MergeType Property

Each menu item has a MergeType property that dictates how it behaves when it (or its parent menu) is merged with another menu item. Its possible values, provided through the MenuMerge enumeration, are shown in Table 15.1.

Table 15.1. MergeType Property Settings
Setting Purpose
Add The menu item is added to the collection of menu items with which it is being merged. This is the default value.
MergeItems Submenu items under this menu item are merged with submenu items in the same position in the merged menu’s hierarchy.
Remove This menu item will not be included in the merged menu.
Replace This menu item replaces a menu item in the same position in the merged menu.

The MergeOrder Property

If a menu item is to be merged with or added to another menu, the MergeOrder property specifies which position the item will occupy in the merged menu. This is a zero-based property, so a menu item whose MergeOrder property is set to 0 will appear first in the merged menu, an item with a MergeOrder value of 1 will appear second, and so forth.

The MDIList Property

The MDIList property is a special property that specifies that a menu item is to show a list of the Text property for all MDI child forms that are displayed inside the MDI parent form containing this menu item. The list is automatically maintained by Windows and can display up to nine MDI child forms at once. If the user clicks one of the listed child forms at runtime, that child form is automatically brought to the front and given the focus. If more than nine child forms are open, a More Windows menu item, which leads to a dialog box showing all child windows, will be displayed.

Creating the Parent Window’s Menu

Let’s continue building the interface of our sample application’s parent form by setting up its menu structure:

1.
Drag a MainMenu control to the form. (Refer to Chapter 10 if you need assistance in working with MainMenu controls.) Create two top-level menus, as follows:

Name Text Shortcut
mnuFile File 
mnuWindow Window 

2.
Create four menu items under the File menu, as follows:

Name Text Shortcut
mnuFileNew New Ctrl+N
mnuFileOpen Open Ctrl+O
mnuFileSep - (hyphen) 
mnuFileExit Exit 

3.
Create four menu items under the Window menu, as follows:

Name Text Shortcut
mnuWindowTileH Tile Horizontal 
mnuWindowTileV Tile Vertical 
mnuWindowCascade Cascade 
mnuWindowSep - (hyphen) 

4.
Display the properties window for mnuWindow and set its MDIList property to True. As you will see in a moment, this will automatically create a window list beneath this menu item.

5.
Set the MergeType property of mnuFile to MergeItems. This will allow the child’s menu items to be combined with those of the parent.

6.
Set the MergeOrder property for mnuFileSep and mnuFileExit to 98 and 99, respectively. This will ensure that the Exit menu item appears at the end of the menu (below a separator bar), which is the Windows standard, even when merged.

Coding the Exit Menu

The first bit of code that you will write is to be executed when the user selects Exit from the File menu. You will simply close the parent form with the Close method when the user wants to exit; in turn, any open child windows will be closed before the parent form is closed. This gives each child window the chance to ensure that the data contained in it is saved, as we will see later in this chapter. Code the Exit menu as follows:

1.
Right-click frmParent in the Solution Explorer window; then select View Code to open its Code window.

2.
In the Code window’s Class Name box, select the mnuFileExit menu item; then in the Method Name box, select the Click event. This will create a shell Click event handler for you to fill in with code.

3.
Add the following line of code to the mnuFileExit_Click event handler:
Me.Close() 



..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.154.121