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. C# also lets you create multiple-document interface (MDI) programs. A MDI program contains one parent window (also called a container) and one or more child windows . A classic example of a MDI program is Microsoft Word 95 (200 behaves slightly different, depending on how it's set up). When you run Word 97, a single parent window appears. Within this parent window, you can open any number of documents, which appear in child windows. In a 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 Word running with a number of child document windows open.

Figure 6.19. MDI applications consist of a single parent window and one or more child windows.

graphics/06fig19.gof


graphics/bookpencil.gif

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. Create a new Windows Application named MDI Example. Change the name of the default form to fclsMDIParent, change its Text property to MDI Parent, update the entry point, 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 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.

Create a new form by choosing Add Windows Form from the Project menu. Name the form fclsChild1.cs and change its Text property to Child 1. Add a third form to the project in the same way. Name it fclsChild2.cs and set its Text property to Child 2. Any form can be a child form (except, of course, a MDI parent form). To make a form a child form, you set its MDIParent property to a form that is defined as a MDI container.

Make sure the fclsMDIParent parent form is visible in the form designer. If it's not, you can display it by double-clicking it in the Solution Explorer. Next, double-click the form to access its default event ”the Load event. Enter the following code:

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

By now, you should know what this code does. The first statement creates a new object variable of type fclsChild1 and initializes it to hold a new instance of a form. The last statement simply shows the form. What we're interested in here is the middle statement. It sets the MdiParent property of the form to the current form ( this always references the current object), which is a MDI parent form because its IsMdiContainer property is set to true. When the new form is shown, it's shown as a MDI child. Save your work and then press F5 to run the project. 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 and would not be bound by the confines of the parent (it wouldn't be a child window).

Figure 6.20. Child forms appear only within the confines of the parent form.

graphics/06fig20.jpg


Stop the project by choosing Stop Debugging from the Debug menu. Display the Solution Explorer and double-click the fclsChild1 form in its designer to display it in the designer. Add a button to the form and set its properties as follows :

Property Value
Name btnShowChild2
Location 105 , 100
Size 85 , 23
Text Show Child 2

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 two differences exist between this code and the code you entered earlier. First, the objChild variable creates a new instance of the fclsChild2 form rather than the fclsChild1 form. The second difference is how the parent is set. Because the child form is not a parent form, you can't simply set the second child's MdiParent property to this, because this doesn't refer to a parent form. However, you know that this.MdiParent references the parent form 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.

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 may 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.

graphics/06fig21.jpg


graphics/bookpencil.gif

The MDI parent form has an ActiveMdiChild property, which you can use to get a reference to the currently active child window.


graphics/bookpencil.gif

To make the parent form larger when the project is first run, you set the Height and Width properties of the form either at design time or at runtime in the Load event of the form.

One thing about forms that is important to note is that you can create as many instances of a form as you desire . For instance, you could change the code in the button to create a new instance of fclsChild1. The form that would be created would be the same as theform that created it, complete with a button to create yet another instance. Although you probably won't apply this technique because you're just getting started with C#, you may find it quite useful in the future. For instance, if you wanted to create a text editor, you might define the text entry portion as one form but create multiple instances of the form as new text files are opened, much like Word does with documents.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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