Professional Documents
Culture Documents
Programming - 6
A Specialised Training Course
Contents
APPLICATIONS WITH MULTIPLE FORMS ................................................................................................ 2
LOADING, SHOWING, HIDING AND UNLOADING FORMS .......................................................................................... 2
Load Statement .................................................................................................................................................. 2
Show Method ..................................................................................................................................................... 2
Hide Method ...................................................................................................................................................... 2
Unload Statement............................................................................................................................................... 3
Modal forms....................................................................................................................................................... 3
CHOOSING THE INITIAL FORM .................................................................................................................................. 3
FORM OBJECT VARIABLES ...................................................................................................................................... 3
OBJECTS & INSTANCES.................................................................................................................................. 4
OBJECT VARIABLES................................................................................................................................................. 4
Generic and Specific Types................................................................................................................................ 4
FORM OBJECT VARIABLES (REVISITED) .................................................................................................................. 5
Set Statement...................................................................................................................................................... 5
New keyword...................................................................................................................................................... 6
Lifetimes of Forms and object variables............................................................................................................ 6
Screen.ActiveForm............................................................................................................................................. 6
Me keyword........................................................................................................................................................ 7
Forms collection ................................................................................................................................................ 7
SYSTEM OBJECTS .................................................................................................................................................... 8
App Object ......................................................................................................................................................... 8
Clipboard Object ............................................................................................................................................... 8
Debug Object ..................................................................................................................................................... 8
Printer Object .................................................................................................................................................... 8
Screen Object..................................................................................................................................................... 8
MDI - MULTIPLE DOCUMENT INTERFACE ............................................................................................. 9
COMPONENTS .......................................................................................................................................................... 9
Creating MDI Forms ....................................................................................................................................... 10
Creating MDI Child Forms ............................................................................................................................. 10
DEVELOPING AN MDI APPLICATION ..................................................................................................................... 10
MDI at Design Time ........................................................................................................................................ 10
MDI at Run-Time ............................................................................................................................................. 11
Instances of Forms........................................................................................................................................... 11
Arranging MDIChild Forms ............................................................................................................................ 12
Dirty, Dirty! ..................................................................................................................................................... 12
EXERCISES ....................................................................................................................................................... 13
Object Variables
We are familiar now with the concept of Visual Basic Control Objects. We can have a form
called frmMain on which we have a text box called txtName, another called txtAddress,
two command buttons called cmdOK and cmdCancel. We can also add an array of controls
which share the same name and event-procedures, let’s say a pair of option buttons called
optSex( ). By creating this array we have already begun to treat our objects like data types.
Generic and Specific Types
Generic Object Variables can refer to any object of that kind. Generic types must be one of...
• Form
• Control
• MDIForm
A generic Form object variable can therefore refer to any form in that application. A generic
Control object variable can refer to any control, whether it be a TextBox, Timer or Frame.
We will discuss MDI Forms later.
Specific Object Variables refer to only one type of object.
This must be one of the following...
• A specific Form in your application
• A specific Control Type (TextBox or Timer or Frame etc.)
Unload Screen.ActiveForm
Screen.ActiveForm.Hide
Unload Me
Me.Hide
Components
An MDI application requires at least two forms...
• MDI Form - a.k.a. parent form or application window
• MDI Child - document form
To allow multiple documents to be active the application launches more than one instance of
the Child form. It is also possible to have several types of MDI Child form (e.g. a text edit
form and picture edit form) available in a single application. It is also possible to have several
forms that are not MDI Child forms, they are treated as you would in a normal application.
An MDI child form appears within the internal area of the MDI Parent. If the parent is not
large enough scroll bars are automatically implemented. An MDI Parent can only contain
controls that have the Align property, usually a picture box. Additional controls can then be
placed inside this control effectively creating a toolbar.
For simplicity we will adopt the terminology of referring to...
• MDI Form - the parent form, as opposed to
• MDIChild Form
The icons are the same as those automatically assigned to the forms to display if they are
minimised. Although the icons for each window can be changed via their icon property, the
icons on the project window do not change.
Instances of Forms
Most of what we discussed earlier about multiple instances of standard forms can be applied
to MDI Child forms (form variables, Me keyword, collections and arrays). We do not need to
consider the subject of multiple MDI Parent Forms as we can have only one Form Type that
is an MDI form in an application and only one instance of that type.
We need to consider how to launch MDI Forms however. This can be done in several ways...
• Set the MDI Form to be the start-up form
• Load a new MDIChild form, the MDI form is loaded automatically
• Load it from Sub Main or another procedure using code
An MDI application will often include a Window menu that includes options for each of the
above as well as a window list for switching active forms.
Dirty, Dirty!
When you have several documents / forms active you should consider placing safeguards that
warn you if you try to unload a form with data that hasn’t been saved.
One recommended technique is to use a state flag, usually implemented as a form level
variable called Dirty. This variable (of type Integer) is set to True when the form data is
altered and to False when the data is saved.
When a form of any type is closed two event procedures are activated...
• QueryUnload - before the Unload is carried out
• Unload - when actually unloaded
In QueryUnload we can check on the value of Dirty and if True allow the user to proceed
without saving, or save then unload or to cancel the unload process.
In the world of MDI we can unload a MDIChild form individually. If we unload the MDI
Form it automatically unloads each MDIChild in turn before unloading itself. The whole
process can be cancelled at any stage by a MDIChild form’s QueryUnload.
Exercise 1
This exercise shows how to pass a specific control type to a procedure, in this case a
Command Button type.
1 Place four command buttons on your form.
2 Name them cmd1 to cmd4 (do not place them in a control array). Give them captions
of "1" to "4"
3 Create a general procedure as follows...
Sub Toggle_Button (PassedButton As CommandButton)
PassedButton.Enabled = Not PassedButton.Enabled
End Sub
4 Add code to each buttons click procedure that calls our Toggle_Button procedure
passing a reference to another button similar to...
Toggle_Button cmd3
So that...
cmd1_click passes a reference to cmd3
cmd2_click passes a reference to cmd1
cmd3_click passes a reference to cmd4
cmd4_click passes a reference to cmd2
5 Run your code. Save it as toggle.frm and toggle.vbp.
Sub update_list ()
listform!List1.Clear
For n = 0 To Forms.Count - 1
listform!List1.AddItem Forms(n).Caption
Next n
End Sub
4 Now create another form, this will be the one we are going to load multiple instances
of at run-time. Place two command buttons on the new form and use the following
property settings.
Sub Form_Load ()
spawned = 0
End Sub
Sub Command1_Click ()
Dim NewForm As formPrototype
Set NewForm = New formPrototype
spawned = spawned + 1
NewForm.Caption = Me.Caption & "." & spawned
NewForm.Move Left + 360, Top + 360
NewForm.Show
update_list
End Sub
Sub Command2_Click ()
Unload Me
update_list
End Sub
8 Run your application. Try cloning and killing various combinations of forms. Move
the forms about to ensure you don't lose any.
Exercise 3 - taking it further
MDIvars
For this exercise you should take a copy of the previous example's files and alter it to become
an MDI application where the prototypes are child forms. You will need to create a new
MDIForm and make the prototype form an MDI child form (look for the property).
Try adding a menu that shows the list of active child windows. How about a quit option too.
While you are about it why not get rid of those space consuming buttons and create a menu
option for loading and unloading instances.
Add a text box to your child forms to prove that they are independent and have their own
data. So that you can enter a decent amount of text make it multiline and add the code to
make it fill the child window.
If you've got this far just consider how much you've done. How long would it have taken you
using C, C++ or Pascal?