All the projects you've created so far have been single document interface (SDI) projects. In SDI programs, every form in the application is a peer of all other forms; no intrinsic hierarchy exists between forms. Visual C# also lets you create multiple document interface (MDI) programs. An MDI program contains one parent window (also called a container) and one or more child windows. A classic example of an MDI program is Adobe Photoshop. When you run Photoshop, a single parent window appears. Within this parent window, you can open any number of documents, each appearing in its own child window. In an MDI program, all child windows share the same toolbar and menu bar, which appears on the parent window. One restriction of child windows is that they can exist only within the confines of the parent window. Figure 6.19 shows an example of Photoshop running with a number of child document windows open. Figure 6.19. Le Collage! MDI applications consist of a single parent window and one or more child windows.By the Way MDI applications can have any number of normal windows (dialog boxes, for example) in addition to child windows. You're now going to create a simple MDI project. Follow these steps to create the project:
By now, you should know what the first and last statements do: The first statement instantiates a new object whose type is fclsChild1. The last statement shows the form nonmodally. What we're interested in here is the second statement. It sets the MdiParent property of the form to the current form (this always references the current object), which is an MDI parent form because its IsMdiContainer property is set to True. When the new form is displayed, it's shown as an MDI child. Press F5 to run the project now and notice how the child form appears on the client area of the parent form. If you size the parent form so that one or more child windows can't fully be displayed, scrollbars appear (see Figure 6.20). If you were to remove the statement that set the MdiParent property, the form would simply appear floating over the parent form (because it wouldn't be a child) and therefore wouldn't be bound by the confines of the parent. Figure 6.20. Child forms appear only within the confines of the parent form.
Stop the project by choosing Debug, Stop Debugging from the menu and follow these steps:
Did you Know? The MDI parent form has an ActiveMdiChild property, which you can use to get a reference to the currently active child window. By the Way To make the parent form larger when the project is first run, you would set the Size.Height and Size.Width properties of the form either at design time or at runtime in the Load event of the form. One thing to keep in mind about forms is that you can create as many instances of a form as you want. Managing multiple instances of the same form gets tricky, however, and is beyond the scope of this book. If MDI forms still confuse you, don't worry. Most of the applications you'll write as a new Visual C# programmer will be SDI programs. As you become more familiar with creating Visual C# projects in general, start experimenting with MDI projects. Remember, you don't have to make a program an MDI program simply because you can; make an MDI program if the requirements of the project dictate that you do so. |