An MDl application must have at least two Forms, the parent Form and one or more child Forms. Each of these Forms has certain properties. There can be many child Forms contained within the parent Form, but there can be only one parent Form.
The parent Form may not contain any controls. While the parent Form is open in design mode, the icons on the Toolbox aren’t disabled, but you can’t place any controls on the Form. The parent Form can, and usually does, have its own menu.
To create an MDI application, follow these steps:
1. Start a new project and .then choose Project >Add MDl Form to add the parent Form.
2. Set the Form’s caption to MDI Window.
3. . Choose Project> Add Form to add a regular Form.
4. Make this Form the child Form by setting its MDI Child property to True. To denote that-this is a child Form, set its Caption property to MDl Child Form.
Visual Basic automatically associates this new Form with the parent Form. nus child Form can’t exist outside the parent Form; in other words, it can only be opened within the parent Form.
Built-In Capabilities of MDI
Notice that the window’s caption is now the parent Form’s caption, followed by the caption of the child Form. You can also move the child Form outside the parent Form, in which case, the appropriate scroll bars will be attached to the parent Form. In addition, both the child window and the MDl Form have a Control . menu (which you can open by clicking the icon in the upper-left comer) and their own Minimize, Normal, and Close buttons (in the upper-right comer).
Clicking the child Form’s Minimize button reduces the child Form to an icon, but always within the parent Form. You’ll never see a child Form’s icon on the Desktop’s status bar, even if its Show ln Taskbar property is set to True. To restore the minimized Form to its original size, double-click the icon. A child window is usually minimized instead of being closed to make room for other documents. In short, the child Form behaves like a regular Form on the Desktop, only its “desktop” is the parent window.
I have just demonstrated the basic operations of an MOl application without writing a single line of code. These capabilities are built into the language and are available to your applications through the settings of certain properties.
To see how useful and powerful MOl applications can be, let’s add a second child Form to the application and throw in a few commands. Follow these steps:
1, Add one more Form to the project, and make it an MDI child Form by setting its MOIChild property to True.
2,’ Set the Form’s Caption property to MDI Child 2.
If you.run the application now, you won’t see the second child’Form. This makes perfect sense because you must be able to open and close child Forms under program control.
3, To display the second Form, you need to add the following line behind a menu command or in the MOl Form’s Load event so that both Forms display when the MDI Form is loaded:
Form2.Show
In a later example, you’ll see how to design menus for MDl applications with commands for opening and closing child Forms.
If you run this small MOl program now, its main window will look much more like an MDI application, thanks to its two child Forms. You can add more child Forms and open them when the MOl Form is loaded with the Show method.
By default, the first child Form is displayed when the MDI Form is loaded. You can also start an MDI application without showing any child Forms, by setting the MOl Form’s AutoShowChildren property to False. In this case, you must either provide a menu command that enables the user to open new documents in child Forms . or load one or more child Forms from within your code.
Parent and Child Menus
MDI Forms can’t contain objects other than child Forms, but MDI Forms do have their own menus, However, because most of the operations of the application have meaning only if there is at least one child Form open, there’s a peculiarity about the MDI menus. The MDI Form usually has a menu with a couple of commands for loading a new child Form and quitting the application. The child Form can have any number of commands in its menu, depending on the application. When the child Form is loaded, the child Form’s menu replaces the original menu on the MDI Form.
VB6 at Work: The MDI Demo1 Form
To see how child Form menus replace the MDI Form’s menu; let’s design an MDI Form with a simple menu. Start a new project, add an MDI Form, and design a menu that has the following structure:
MDIMenu Menu caption –
MDIOpen opens a new child Form
MDIExit Terminates the application.
Then design the following menu for the child Form:
ChildMenu Menu caption
Child Open Opens a new child Form
Child Save Saves the document in the active child Form
Child Close Closes the active child Form.
You don’t need to add any code to the previous menus at this point. If you run this , application now (it’s the MDI Demo1 application in this chapter’s folder on the CD), you’ll see the child Form’s menu on the MDI Form’s menu bar. If you close the child Form, the original menu of the MDI Form will replace the child Form’s menu. The reason for this behavior should be obvious. The operations available through the MDl Form are quite different from the operations of the child window. Moreover, each child Form shouldn’t have its own menu.
A different menu on the container (the MDI Form) and a different menu on the individual child Forms would confuse the user. You should be aware of this idiosyncrasy when designing menus for MDl applications. The common practice is to attach a limited menu to the MDl Form, which is repeated in the child Form’s menu.
The Window Menu
A few more features are built into MDl Forms. These features aren’t available through properties; but with a minimum of coding you can include them in your programs. All MDl applications in the Windows environment have a submenu called Window that contains two groups of commands. The first group of commands positions the child windows on the MDI Form, and the second group consists of the captions of the open child windows. With the commands on this menu, you can change the arrangement of the open windows (or the icons of the minimized windows) and activate any child window.
Let’s design an MDI Form that maintains its Window menu:
1. . Select a child Form in the Project window and design a menu for this window.
2. Add a single command named Window.
3. Check the WindowList checkbox in the Menu Editor, as shown.
You have just tell the MDI application to maintain a list of all child windows that appear on the Form and display their names in the Window menu. The active child window is denoted with a check mark next to its entry in the menu. Even the separator line between the two groups of commands is placed there automatically.
The Arrange Property
Windows offers three ways of arranging the windows on an MDI Form. You can cascade them, tile them vertically, or.tile them horizontally. Of course, the user can resize and move the windows around, but the automatic placement comes in’ handy when the MDI Form becomes messy and the user can no longer easily locate the desired window. The placement of the child windows on the Form is controlled with the Arrange property, which can take one of the values shown in Table.
The Arrange property isn’t available at design time, and it’s usually set by the commands in the Window menu. The first three values in table concern the arrangement of the windows when they are in normal state; the last value concerns the arrangement of-the icons of minimized windows. As the user minimizes and maximizes the windows, it’s possible for the icons to end up in the four corners of the MDl From some of them even outside the Form’s visible area..
When the Arrange property is set to 3, the icons are placed next to the another in the lower-left comer of the MDl Form. This property acts as a method in the sense that it·causes an action when it’s set. To see how the Arrange property works, design a menu with the following structure (the menu belongs to the child Form of the application, not the MDl Form):
Don’t do anything about the list of child windows; just check the Window List checkbox in the Menu Edit window for the Window menu command. If you run the application now, you’ll see the name of the child Form attached to the Window menu. This is taken care of by Visual Basic, which maintains the list of current child windows and displays their names at the bottom of the Window menu. The name of the active child window is checked in this list, and you can select another window by clicking its name in the Window list. To implement the commands that rearrange the windows on the MDl Form, you must provide a few lines of code. The code 15 really trivial and it sets the MDl Form’s Arrange property. For example, the code for the Cascade command is shown next.
the subroutines for the Tile Vertically,The Horizontally, and Arrange Icons commands are similar. Run the application and check out all the features that are already built into the MDI Form (or open the MDI Demo 2 application in this chapter’s folder on the CD). Move the child windows around, minimize and maximize them, arrange them and their icons on the MDI Form, and switch from one to the other with the help of the Window menu’s commands.
With.just a few lines of code, you can create a good deal of functionality. By the way, although the name of this menu doesn’t need to be Window (any menu name will do)
Despite its simplicity, you may have-noticed a problem with the approach just described. You may be wondering if you have to design a separate child Form for each document you need to display on the MDI Form. And, even worse, do you have to refer to each child window with a different name? It would be so much easier if you could declare an array of child Forms and access them with an index. Actually, this is the way child Forms are handled, and this is the next topic we’ll explore.