Windows MDI Application

  1. Create the new project.
  2. Open Visual Studio .NET, and select File > New > Project… > Visual C# Projects > Windows Application.
  3. Name the project UsingMDIApp and select a directory location in which to save the project.
  4. Click OK. Visual Studio.Net will load the new solution and a form labeled Form1 will appear.
  5. Rename the source code file.
  6. Click the Solution Explorer in the toolbar or select View > Solution Explorer.
  7. Right-click on Form1.cs and rename it to UsingMDI.cs.
  8. Right-click on UsingMDI.cs and select View Code.
  9. Replace all occurrences of Form1 with UsingMDI.
  10. Go back to Design View by clicking on the UsingMDI.cs[Design]* tab.
  11. Set the form’s properties.
  12. Select the form.
  13. Click the Properties icon in the toolbar or select the View menu’s Properties Window command.
  14. Set the form’s Name to UsingMDI.
  15. Set the form’s Text property to Using MDI.
  16. Set the form’s IsMDIContainer property to true. Note how the form changes appearance.
  17. Resize the form to a larger size by clicking and dragging the form’s sizing handles.
  18. Create a child form class.
  19. Right-click the project UsingMDIApp in Solution Explorer.
  20. Select Add > Add Windows Form. Use the defaults, but name the file MyChildForm.cs.
  21. Click Open.
  22. Right-click on the child form and select View Code.
  23. Find the constructor MyChildForm and type string title in the parameter list.
  24. 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.

  1. Create a menu.
  2. Click on UsingMDI.cs [Design]* tab.
  3. 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.

  1. Click the menu bar icon to start Menu Designer.
  2. Click the Type Here textbox and type &File
  3. Beneath it, type &New and beneath that &Exit.
  4. Beside File, type Format.
  5. Beneath Format, type Cascade, then Tile Horizontal, then Tile Vertical.
  6. Now select the menu item File. In the Properties menu, change its Name to fileMenuItem.
  7. Do the same for all menu items as follows: newMenuItem, exitMenuItem, formatMenuItem, cascadeMenuItem, tileHorizMenuItem, tileVertMenuItem.
  8. 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.
  1. To add the child form to the parent.
  2. Go back to Design View by clicking on the UsingMDI.cs[Design]* tab.
  3. Double-click the menu item New.
  4. 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();

  1. Save the project and the C# file.
  2. Select File > Save All
  3. Run the project.
  4. Click on Build > Build Solution.
  5. Click the Start button (the blue triangle) or click on Debug > Start.
  6. Create three new child windows, by selecting File > New.
  7. Close the second child window by clicking its close box.
  8. Terminating execution.
  9. Exit the application by selecting File > Exit or click the running application’s Close button.