The MDI NotePad sample application is a simple text editor similar to the NotePad application included with Microsoft Windows. The MDI NotePad application, however, uses a multiple-document interface (MDI). At run time, when the user requests a new document (implemented with the New command on the application's File menu), the application creates a new instance of the child form. This allows the user to create as many child forms, or documents, as necessary.
To create a document-centered application in Visual Basic, you need at least two forms — an MDI form and a child form. At design time, you create an MDI form to contain the application and a single child form to serve as a template for the application's document.
To create your own MDI NotePad application
The project should now contain an MDI form (MDIForm1) and a standard form (Form1).
Object | Property | Setting |
MDIForm1 | Caption | MDI NotePad |
Form1 | Caption MDIChild |
Untitled True |
Text1 | MultiLine Text Left Top |
True (Empty) 0 0 |
Caption | Name | Indented |
&File | mnuFile | No |
&New | mnuFileNew | Yes |
Private Sub mnuFileNew_Click ()
' Create a new instance of Form1, called NewDoc.
Dim NewDoc As New Form1
' Display the new form.
NewDoc.Show
End Sub
This procedure creates and then displays a new instance (or copy) of Form1, called NewDoc. Each time the user chooses New from the File menu, an exact duplicate (instance) of Form1 is created, including all the controls and code that it contains.
Private Sub Form_Resize ()
' Expand text box to fill the current child form.
Text1.Height = ScaleHeight
Text1.Width = ScaleWidth
End Sub
The code for the Form_Resize event procedure, like all the code in Form1, is shared by each instance of Form1. When several copies of a form are displayed, each form recognizes its own events. When an event occurs, the code for that event procedure is called. Because the same code is shared by each instance, you might wonder how to reference the form that has called the code — especially since each instance has the same name (Form1). This is discussed in "Working with MDI Forms and Child Forms," later in this chapter.
Tip The Mdinote.vbp sample application contains examples of many MDI techniques besides those mentioned in this chapter. Take some time to step through the example code to discover these techniques. The Sdinote.vbp sample application is an implementation of the same application converted to the SDI style; compare the two samples to learn the differences between MDI and SDI techniques.