Windows MDI Application 1. Create the new project.

advertisement
Windows MDI Application
1. Create the new project.
a. Open Visual Studio .NET, and select File > New > Project… > Visual C#
Projects > Windows Application.
b. Name the project UsingMDIApp and select a directory location in which
to save the project.
c. Click OK. Visual Studio.Net will load the new solution and a form labeled
Form1 will appear.
2. Rename the source code file.
a. Click the Solution Explorer in the toolbar or select View > Solution
Explorer.
b. Right-click on Form1.cs and rename it to UsingMDI.cs.
c. Right-click on UsingMDI.cs and select View Code.
d. Replace all occurrences of Form1 with UsingMDI.
e. Go back to Design View by clicking on the UsingMDI.cs[Design]* tab.
3. Set the form’s properties.
a. Select the form.
b. Click the Properties icon in the toolbar or select the View menu’s
Properties Window command.
c. Set the form’s Name to UsingMDI.
d. Set the form’s Text property to Using MDI.
e. Set the form’s IsMDIContainer property to true. Note how the form
changes appearance.
f. Resize the form to a larger size by clicking and dragging the form’s sizing
handles.
4. Create a child form class.
a. Right-click the project UsingMDIApp in Solution Explorer.
b. Select Add > Add Windows Form. Use the defaults, but name the file
MyChildForm.cs.
c. Click Open.
d. Right-click on the child form and select View Code.
e. Find the constructor MyChildForm and type string title in the
parameter list.
f. After the //TO DO comment, add the line Text = title;
To create instances of child forms, we need a control with an event handler on
the parent form – we will use a menu.
5. Create a menu.
a. Click on UsingMDI.cs [Design]* tab.
b. Drag a MainMenu control from the Toolbox onto the parent form.
This creates a menu bar on top of the form with an icon beneath the form.
c. Click the menu bar icon to start Menu Designer.
d. Click the Type Here textbox and type &File
e. Beneath it, type &New and beneath that &Exit.
f. Beside File, type Format.
g. Beneath Format, type Cascade, then Tile Horizontal, then Tile Vertical.
h. Now select the menu item File. In the Properties menu, change its Name
to fileMenuItem.
i. Do the same for all menu items as follows: newMenuItem, exitMenuItem,
formatMenuItem, cascadeMenuItem, tileHorizMenuItem,
tileVertMenuItem.
j. On the parent form, double-click the menu item Exit. (Menu items
generate a click event when selected by users during application
execution.) Type in the code Application.Exit(); within the
exitMenuItem_Click method.
6. To add the child form to the parent.
a. Go back to Design View by clicking on the UsingMDI.cs[Design]* tab.
b. Double-click the menu item New.
c. In the newMenuItem_Click method, type the following code.
//create a new child
MyChildForm myChild = new MyChildForm (“Child” );
//set child’s parent
myChild.MdiParent = this;
//display child
myChild.Show();
7. Save the project and the C# file.
a. Select File > Save All
8. Run the project.
a. Click on Build > Build Solution.
b. Click the Start button (the blue triangle) or click on Debug > Start.
c. Create three new child windows, by selecting File > New.
d. Close the second child window by clicking its close box.
9. Terminating execution.
a. Exit the application by selecting File > Exit or click the running
application’s Close button.
Download