Visual Basic Concepts

The MDI NotePad Application

See Also

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

  1. From the File menu, choose New Project.

  2. From the Project menu, choose Add MDI Form to create the container form.

    The project should now contain an MDI form (MDIForm1) and a standard form (Form1).

  3. Create a text box (Text1) on Form1.

  4. Set properties for the two forms and the text box as follows.
    Object Property Setting
    MDIForm1 Caption MDI NotePad
    Form1 Caption
    MDIChild
    Untitled
    True
    Text1 MultiLine
    Text
    Left
    Top
    True
    (Empty)
    0
    0

  5. Using the Menu Editor (from the Tools menu), create a File menu for MDIForm1.
    Caption Name Indented
    &File mnuFile No
    &New mnuFileNew Yes

  6. Add the following code to the mnuFileNew_Click procedure:
    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.

  7. Add the following code to the Form_Resize procedure for Form1:
    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.

  8. Press F5 to run the application.

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.