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:
Understand how MDI parent and child forms operate, and differ from each other.
Create a MDI parent form with pull-down menu.
Create a MDI child form with its own menu.
Load/create and unload child forms when required during run-time.
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.
Create an MDI parent form by selecting Add MDI Form from the Project menu.
Create a new (normal) form by clicking on the new form icon .
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.
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.
A MDI form cannot be modal.
When a child form is minimized or maximized it does so within the confines of its parent window.
When a child form is maximized its form caption is combined with the form caption of its parent window.
Child forms cannot be hidden. To get rid of a child form you need to unload it.
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.
Create a MDI parent form and set its various properties (i.e. Caption).
Create a pull-down menu for the parent form with the following structure:
File ...New ...- ...Exit
Create a normal form and change it into a MDI child form. To do this go to the properties window.
Create a pulldown on the child form with the following structure:
File ...New ...Close ...- ...Exit Window ...Cascade ...Tile ...Arrange Icons
Place any controls onto the child form such as text boxes, picture boxes or command buttons.
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 childs menu replaces the parents menu when it is displayed you will need the above code in both menus New option.
Enter Unload Me in the Close option on the child forms menu. The reserved word Me refers to the currently focused form.
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
On the Window entry on the child forms 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.