Creating MDI Forms


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:

1.

Choose File, New Project from the menu to display the New Project dialog box (note how this is a modal form).

2.

Enter the name MDI Example and click OK to create the project.

3.

Right-click Form1.cs in the Solutions Explorer window and choose Rename from the shortcut menu. Change the name of the form to fclsMDIParent.cs. Next, change the form's Text property to MDI Parent, and change its IsMdiContainer property to True. (If you don't set the IsMdiContainer property to True, this example won't work.)

The first thing you'll notice is that Visual C# changed the client area to a dark gray and gave it a sunken appearance. This is the standard appearance for MDI parent windows, and all visible child windows appear in this area.

4.

Create a new form by choosing Project, Add Windows Form from the menu. Name the form fclsChild1.cs and change its Text property to Child 1.

5.

Add a third form to the project in the same way. Name it fclsChild2.cs and set its Text property to Child 2.

6.

Click Save All on the toolbar.

7.

Double-click fclsMDIParent.cs in the Solution Explorer to show the parent window in the designer.

8.

Next, double-click the form to access its default eventthe Load event. Enter the following code:

fclsChild1 objChild = new fclsChild1(); objChild.MdiParent = this; objChild.Show();


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:

1.

Display the Solution Explorer, and double-click the fclsChild1 form to display it in the designer.

2.

Add a button to the form and set the button's properties as follows:

Property

Value

Name

btnShowChild2

Location

105,100

Size

85,23

Text

Show Child 2


3.

Double-click the button to access its Click event, and then add the following code:

fclsChild2 objChild = new fclsChild2(); objChild.MdiParent = this.MdiParent; objChild.Show();


This code shows the second child form. Note that differences exist between this code and the code you entered earlier. You can't set the second child's MdiParent property to this because this refers to the current form (fclsChild1, which is not an MDI container). However, you know that this.MDIParent references the parent form of a child because this is precisely the property you set to make the form a child in the first place. Therefore, you can simply pass the parent of the first child to the second child, and they'll both be children of the same form.

By the Way

Any form can be a child form (except, of course, an MDI parent form). To make a form a child form, set its MDIParent property to a form that's defined as an MDI container.

4.

Press F5 to run the project now. You'll see the button on the child form, so go ahead and click it (if you don't see the button, you might have mistakenly added it to the second child form). When you click the button, the second child form appears. Notice how this is also bound by the constraints of the parent form (see Figure 6.21).

Figure 6.21. Child forms are peers with one another.


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.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net