Site Search

Lab 7: Multiple-Document Interface (MDI) Applications

Learning Objectives for Lab 7

After completion of today's lab, you should be able to

Window Management Schemes

Microsoft Windows provides several window management schemes:

  1. single-document interface
  2. multiple-document interface
  3. workbooks
  4. projects

Choosing the right scheme requires thinking about the users, their skill levels, the tasks, the display space, etc.

A multiple-document interface contains a single primary window, called the parent, and a set of child windows contained in the parent. It was popularized by Microsoft Word 97 in which a single Word application contains multiple text windows that represent the current documents being worked upon.

The advantages include that the child windows share the parent's interface components like menus, toolbars, and status bars. It provides a logical grouping of objects within the larger context of the desktop. And, it also helps in comparisons between similar types of data in multiple windows.

MDIs have fallen out of favor with interface designers in recent years. One disadvantage with MDIs is the difficulty of rapidly switching from one document to another. Tabbed document interfaces is a popular way of providing the advantages of MDIs along with a quick way to switch between open documents.

Creating MDI Parent Forms

With MDI applications, the parent form needs to have its IsMDIContainer property set to True. While you are at it, you can also set the WindowState property to Maximized as it is easiest to manipulate MDI child windows when the parent form is maximized.

Activity: Create a MDI Parent Class

window menu
  1. Create a New Project called WindowsUI in Solution Lab7
  2. Change the name of the Form1 file to MainForm.cs
  3. Change the text displayed in MainForm's title bar to Lab 7
  4. Use the Properties window with MainForm to
    • its isMDIContainer property to True
    • Set the WindowState property to Maximized
  5. Add a MenuStrip control to MainForm and insert the standard menu items
  6. Add a Window menu named windowToolStripMenuItem to your menu before the Help menu
    • Set its Text property to &Window
  7. Add menu items to Cascade, Tile Horizontally, Tile Vertically, Separator line, Close (Ctrl+F4), and Close All items
  8. To create a MDI child window list in your Window menu, set the MenuStrip's MdiWidowListItem property to windowToolStripMenuItem.
  9. Compile and test your program

Creating MDI Child Forms

Child forms in MDI applications are derived from Window's Form class

To add a new window as a MDI child to a MDI application, declare and instantiate the new object in the MDI parent form, set its MdiParent property to the MDI parent object, and then show the form.

   ChildForm myChild = new ChildForm();

   myChild.MdiParent = this;
   myChild.Show();

Activity - Create a MDI Child Class

  1. Add a new Windows form to the project named ChildForm
  2. Place a RichTextBox control in your ChildForm and Dock the RichTextBox in the parent container
  3. Add the event handler method in MainForm for the New menu item under the File menu to instantiate and show a new ChildForm
  4. Compile and test your program

Managing MDI Child Forms

To close the current child window, the .NET environment keeps track of the active window in the property ActiveMdiChild

   this.ActiveMdiChild.Close();

You can manage the Window layout by using the LayoutMdi method with the Parent object.

   this.LayoutMdi(MdiLayout.TileVertical);

The MDI Parent object has a MdiChildren property that provides an array of references to MDI child objects. A possible use of the MdiChildren array is to write a foreach loop to close all the current child windows open.

   foreach (ChildForm window in MdiChildren)
      window.Close();

Activity: Manage your children

  1. Add the event handler method to the Window menu's Close item to close the currently active MDI child window
  2. Add the event handlers to the Windows menu to Cascade, Tile Horizontally, and Tile Vertically your MDI child windows
  3. Add the event handler to the Windows menu to Close All child windows

Child Menus

Font menu

When an MDI child form has a MainMenu component and the child form is opened within an MDI parent form that also has a MainMenu component, then the menu items will merge automatically.

Activity: Add an Font Menu to the ChildForm class

  1. Add an Font MainMenu control to the ChildForm class to allow the user to change the font face and font size
  2. Compile and test your program
   this.richTextBox1.Font = new Font(FontFamily.GenericMonospace, 12f);

In place of merging, one can also replace existing MdiParent menus with Child menus.

Activity: Add an Edit to the ChildForm class

  1. Copy the Edit menu from your MainForm's menu onto your ChildForm menu
  2. Add method handlers for the Cut, Copy, and Paste menu items
  3. Change the Edit menu's MergeAction's property to replace
  4. Compile and test your program

A quick way to provide functionality to edit menu items is to use the TextBox methods (inherited from TextBoxBase)

   richTextBox1.Undo();
   richTextBox1.Cut();
   richTextBox1.Copy();
   richTextBox1.Paste();
   richTextBox1.SelectAll();

URL: http://www/cwu.edu /~gellenbe/446/labs/lab7.php
Author: Ed Gellenbeck, Department of Computer Science, Central Washington University, gellenbe@cwu.edu
Copyright 2006 Ed Gellenbeck, Central Washington University
Last modified: March 22, 2008