Using MDI Forms
This article gives a quick ides of how to use an MDI form with a single form as a template for other documents. It also shows the Arrange function.
Example Project Layout
The example project contains 3 components - an MDI form (mdiMain), the document form template (frmDocument) and the main module.
Creating a Document
The frmDocument is a template which will be used to create all other documents. It is a normal form, with the appropriate controls and code added, and with the MDIChild property set to true - this ensures that the form appears within the MDI form in the project (note that there can only be one MDI form in a project, but many MDI child forms).
The code to create a new document is as follows:
Dim NewFormIndex On Error Resume Next NewFormIndex = UBound(frmDoc) + 1 If Err <> 0 Then NewFormIndex = 0 On Error GoTo 0
ReDim Preserve frmDoc(NewFormIndex) Set frmDoc(NewFormIndex) = New frmDocument frmDoc(NewFormIndex).Caption = "Document " & NewFormIndex frmDoc(NewFormIndex).Show
The On Error Resume Next statement may be new - this causes the runtime to continue execution if an error occurs (therefore making it invisible to the user). You can check the Err system property to determine if an error occurred - the Err variable will be 0 if no error occurred or an error code value if an error does occur. Remember, though, that this variable is set after each statement is executed - therefore make sure you check the error value on the very next line after the statement that you expect the error to occur on, otherwise the value may be incorrect.
In the example above, if the array has never been resized with ReDim, then using the UBound function will cause an error. We can then explicitly set the next index to 0 (and therefore create the first item in the array later in the code).
If you create an array with the declaration:
Private frmDoc() As frmDocument
you are creating a dynamically sizable array. Initially, it has no elements. You use the ReDim statement to resize the array, adding the optional Preserve keyword if you do not want the current contents to be erased on resize.
For the example, the line:
ReDim Preserve frmDoc(NewFormIndex)
increases the size of the array by 1 (as the NewFormIndex is calculated from the UBound function, which returns the upper bound of the array, incremented by 1). At this point, we have created a new placeholder for a frmDocument object (explicitly defined as the type of the array at the top of the module - it does not need to be repeated for each ReDim statement), but not actually created the object - we could not use it yet.
Instantiating the Object
Set frmDoc(NewFormIndex) = New frmDocument
The Set statement is used to assign an object to the object placeholder - the object in question is a new frmDocument.
Now you can use the frmDocument(NewFormIndex) object in exactly the same way as a frmDocument object - but it will be one of potentially many within the MDI form frame.
Arranging Child Forms
MDI Forms offer an easy way to arrange the inner Child forms - using the Arrange method of the MDIForm itself. This method takes a single parameter, defining the type of arrangement required:
|vbCascade||0||Cascades all nonminimized MDI child forms|
|vbTileHorizontal||1||Tiles all nonminimized MDI child forms horizontally|
|vbTileVertical||2||Tiles all nonminimized MDI child forms vertically|
|vbArrangeIcons||3||Arranges icons for minimized MDI child forms|
The following code would arrange all the icons of minimized MDI child forms: