Windows MDI Application
- Create the new project.
- Open Visual Studio .NET, and select File > New > Project… > Visual C# Projects > Windows Application.
- Name the project UsingMDIApp and select a directory location in which to save the project.
- Click OK. Visual Studio.Net will load the new solution and a form labeled Form1 will appear.
- Rename the source code file.
- Click the Solution Explorer in the toolbar or select View > Solution Explorer.
- Right-click on Form1.cs and rename it to UsingMDI.cs.
- Right-click on UsingMDI.cs and select View Code.
- Replace all occurrences of Form1 with UsingMDI.
- Go back to Design View by clicking on the UsingMDI.cs[Design]* tab.
- Set the form’s properties.
- Select the form.
- Click the Properties icon in the toolbar or select the View menu’s Properties Window command.
- Set the form’s Name to UsingMDI.
- Set the form’s Text property to Using MDI.
- Set the form’s IsMDIContainer property to true. Note how the form changes appearance.
- Resize the form to a larger size by clicking and dragging the form’s sizing handles.
- Create a child form class.
- Right-click the project UsingMDIApp in Solution Explorer.
- Select Add > Add Windows Form. Use the defaults, but name the file MyChildForm.cs.
- Click Open.
- Right-click on the child form and select View Code.
- Find the constructor MyChildForm and type string title in the parameter list.
- 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.
- Create a menu.
- Click on UsingMDI.cs [Design]* tab.
- 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.
- Click the menu bar icon to start Menu Designer.
- Click the Type Here textbox and type &File
- Beneath it, type &New and beneath that &Exit.
- Beside File, type Format.
- Beneath Format, type Cascade, then Tile Horizontal, then Tile Vertical.
- Now select the menu item File. In the Properties menu, change its Name to fileMenuItem.
- Do the same for all menu items as follows: newMenuItem, exitMenuItem, formatMenuItem, cascadeMenuItem, tileHorizMenuItem, tileVertMenuItem.
- 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.
- To add the child form to the parent.
- Go back to Design View by clicking on the UsingMDI.cs[Design]* tab.
- Double-click the menu item New.
- 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();
- Save the project and the C# file.
- Select File > Save All
- Run the project.
- Click on Build > Build Solution.
- Click the Start button (the blue triangle) or click on Debug > Start.
- Create three new child windows, by selecting File > New.
- Close the second child window by clicking its close box.
- Terminating execution.
- Exit the application by selecting File > Exit or click the running application’s Close button.