VB Logo

Tutorial: Multiple Document Interface Applications


The objective of this tutorial is to learn how to create a multiple-document interface (MDI) application. At the end of this tutorial you should be able to:


Multiple-Document Interface (MDI) allows programmers to create applications that can handle several files (documents) open at once. There is a single parent window and a number of child windows. Although child windows can be minimized or maximized they do so within the confines of their parent window. Word and Excel are examples of MDI applications. If you are not familiar with how MDI applications work please take the opportunity to do so now.



Creating an MDI Application Steps

  1. Create an MDI parent form by selecting ‘Add MDI Form’ from the ‘Project’ menu.

  2. Create a new (normal) form by clicking on the new form icon .

  3. On all forms, apart from the Parent, set their ‘MDIChild’ property to True. This step changes a form from an SDI type to a specific MDI child type.

  4. Repeat steps 2 and 3 until enough child forms have been created. Alternatively, create a parent MDI form and only one child form. Then use the line ‘Dim form_name As New child_form’ when you wish to create a new child. This will create a new instance of the child form dynamically at run-time.



Differences when using MDI

  1. A MDI form cannot be modal.

  2. When a child form is minimized or maximized it does so within the confines of its parent window.

  3. When a child form is maximized its form caption is combined with the form caption of its parent window.

  4. Child forms cannot be hidden. To get rid of a child form you need to unload it.

  5. If the currently focused child form has a pull-down menu then this replaces the MDI parent menu. If a child form does not have a menu but the parent form does then the parent menu is then displayed. This menu replacement behaviour can be seen in MS Word. When all child documents are closed the menu had two items - File and Help. However, as soon as a child document is created the menu changes to include: File, Edit, View, Insert, Format, Tools, Table, Window and Help.



Task

  1. Create a MDI parent form and set its various properties (i.e. Caption).

  2. Create a pull-down menu for the parent form with the following structure:

      File
      ...New
      ...-
      ...Exit
    
  3. Create a ‘normal’ form and change it into a MDI child form. To do this go to the properties window.

  4. Create a pulldown on the child form with the following structure:

      File
      ...New
      ...Close
      ...-
      ...Exit
      Window
      ...Cascade
      ...Tile
      ...Arrange Icons
    
  5. Place any controls onto the child form such as text boxes, picture boxes or command buttons.

  6. On the ‘New’ menu command enter the code to create a child form.

    Example:

    Private Sub mnuNew_Click()
      Dim frmNew As New frmChild       ' Create a new instance of a child form
      frmNew.Show
      frmNew.Caption = "MDI Child #" & (forms.Count - 1)
    
    End Sub
    

    The above code creates a new instance of the form named ‘frmChild’. The newly created form is called ‘frmNew’ and the next line down displays it using the Show command.

    Note: Because the child’s menu replaces the parent’s menu when it is displayed you will need the above code in both menu’s ‘New’ option.

  7. Enter ‘Unload Me’ in the ‘Close’ option on the child form’s menu. The reserved word ‘Me’ refers to the currently focused form.

  8. Write the code for the ‘Window’ options such as: cascade, tile and arrange icons. Code to do this:

      frmMDIParent.Arrange 0             ' Cascade
    
      frmMDIParent.Arrange 1             ' Tile (horizontally)
    
      frmMDIParent.Arrange 2             ' Tile (vertically)
    
      frmMDIParent.Arrange 3             ' Arrange icons
    
  9. On the ‘Window’ entry on the child form’s pull-down menu check the ’WindowList’ box (see below).


    With this option checked VB will add a list, at the end of ‘Window’, of all currently loaded child forms. Clicking on any form listed automatically makes the required child focused.




Download MDI Form example

Tutorial 13 (Running Crystal Reports)
Menu
Tutorial 15 (Incorporating Sound)