Multiple Document Interface (MDI)
The multiple document interface (MDI) is another
common item found in Windows applications. The MDI
allows you to create an application that has many forms
that are contained within one main form. Applications like
Microsoft Word, where you can open several different
documents within the main Word program, are MDIs.
The forms that make up an MDI (em-dee-eye) project are
referred to as parent and child forms. The main
application is referred to as the parent, and all of the
forms contained within the application are called the
children.
Parents and Children
There can be only one MDI parent form per
application. This cuts down on the possible
confusion that could result from having more than
one form in charge of the rest. You create an MDI
parent form by setting the appropriate property of the
form in the Properties window. When you create an
MDI form, the Properties window refers to it
specifically as an MDI form rather than just a form.
To have the application recognize that the other
forms are children of the MDI form, you set their
MDIChild property to True.
Programming Skills
A major problem that programmers run into is that
they tend to design for their own use. It may seem
obvious to you or another programmer how a
program should process data and how a user
accesses and enters that data. However, the
end‑user typically does not have the same
perspective. Therefore, when designing a program, it
is essential that you plan for alternate approaches.
Murphy's law here is, “If there is one thing that would
crash a program, users will find it."
Creating an MDI Application
In order to create an MDI application, you must create
an MDI form. Creating an MDI form is similar to
creating any other new form. You populate the form
with controls from the Toolbox and build a menu. Then
change the form’s IsMDIContainer property to True.
After the MDI container or parent form is created, you
create a template from which child forms are created.
You add a second form to the project and populate it
with controls from the Toolbox and provide a menu.
This second form is the template used to create child
forms while the application is running.
Me
While the application is running, the parent
form can be referred to by the name Me. This
is a special variable in Visual Basic. Me holds
the name of the form that is currently active.
Thus, the Me takes different values
depending on the active form.
Step-by-Step 3.5
'Create a new instance (copy) of Form2
Dim NewMDIChild As New Form2()
'Designate the Child form's parent
NewMDIChild.MDIParent = Me
'Display the new form.
NewMDIChild.Show()
Cascaded
Child
Forms
Child Forms
Tiled
Horizontally
Child
Forms
Tiled
Vertically
Child Form Icons Arranged on the
Parent Form
Step-by-Step 3.6
Form1.LayoutMDI(MDILayout.Cascade)
Form1.LayoutMDI(MDILayout.TileHorizontal)
Form1.LayoutMDI(MDILayout.TileVertical)
Form1.LayoutMDI(MDILayout.ArrangeIcons)
Adding a Form
A unique form is created in the design phase
of the project. Even though you work with the
form in the design phase, the form must be
called into existence by program code. If the
name of the added form is About.vb, the
code to create a new instance of the form is
Dim NewAbout As New About().
The New Form
Once a new form is added, the Show method
of the form is called to display it. The Hide
method hides the form without unloading it
from memory. If necessary, the form’s
Unload method is used to unload the form
from memory. Unlike the child forms that
appear wholly contained within the parent
form, the new form is displayed
independently.