Understanding MDI Applications in .NET
Form Basics
Many Windows applications use a multiple document interface (MDI). I’m sure you’ve used one yourself—Excel and Photoshop are just a couple of examples of commercial MDI apps. But what exactly is special about MDI?
The MDI interface is designed for applications that need to have two or more documents open at the same time. The documents are independent of each other and have different content, but they’re the same type of document, such as an Excel workbook or a Photoshop image. An MDI application provides a single parent window—called the MDI parent form—with each open document represented by a child form. This arrangement has some special characteristics:
- Child forms are restricted to the parent form’s client area.
- Minimizing/restoring the parent automatically minimizes/restores the children.
- The application’s menu and/or toolbar are hosted in the parent form.
- Only one child can be active at a time, and user input and commands are automatically routed to that active child form.
An MDI child form can contain controls and other components, just like a regular form. The parent form is typically limited to menu and toolbars. In most MDI applications, all child forms are instances of the same class—a class designed to work with the application’s documents, whatever they may be. An MDI parent is not limited to showing child forms; it can also display standard forms/dialog boxes that are not in the special MDI parent/child relationship.
Creating the Parent Form
In most respects, an MDI parent form is like any other form. At design time, all you need to do is set its IsMdiContainer property to True. The form’s background color is automatically set to a dark gray when you do this, but you can change that color if desired.
Creating Child Forms
Nothing different is required when creating a child form. Its child relationship with the MDI parent is set in code when the form is instantiated, not at design time. All that’s required is to set the form’s MdiParent property to refer to the parent form. You’ll see an example later.
Creating a Document List
It’s standard practice for an MDI application to present a list of all open documents from which the user can choose. Traditionally, this is done on a Window menu, and this task can be automated as follows:
- Place a MenuStrip control on the parent form.
- Add a Window item to the MenuStrip control.
- Set the MdiWindowListItem property of the MenuStrip control to refer to the Window menu item.
Yep, it’s that simple. The framework will automatically display a list of all open documents on the Window menu, below any other items on that menu, using whatever names you’ve assigned to the Text property of each form. Don’t you just love it when your programming tool makes your life so much easier?