Chris's Event Programming with Visual Basic course.
Contents
| Introduction | Tool Bars & Control Arrays |
So far we have worked with one form on our projects, it is possible (and desirable in many applications) to use more than one form at a time. Forms are objects and like the controls that you can put on them, have properties and respond to events and may be acted on by methods.
The objective here is to show how an application consisting of two forms could be built. This is a very simple example and doesn't do much but the principle can be applied to more complex problems.
Starting with a new project, click on the Backcolor property, (a value number is displayed) and then on the button with 3 dots. This displays a colour palate as shown below:
By clicking on your chosen colour, the background colour of the form is changed, note that the numerical value of the property is also changed.
Chose New Form from the File menu options, a new form is added to the project (it now is also listed in the project window). Change the background property of this form to a different colour.
We will now make the Form2 become the start up form for the application. This is achieved by choosing Project from the Options menu, selecting Start Up and then choosing Form2 from the combo box. Click on OK button to set the start up option.
We will now navigate between the forms by invoking a click event on form2. This will use the Show Method to display form1. The code should appear as below:-
Sub Form_Click ()
form1.Show
End Sub
Start your application, the form2 loads first. Clicking on the form opens form1 on top of form2. You will find it difficult to get form2 back! Close your application. We can make form2 a little easier to recover by making use of it's dblclick event, as this manipulates no objects, the form will be displayed with no other action taking place. To make form2 easier to manipulate the property WindowState should be changed to the value 2 - Maximised. Run your application again, it is now easy to double-click back on to Form2 as it is Maximised. Close the application by minimising Form2 and closing.
Forms can be re-sized, Maximised, Minimised, Loaded, Unloaded, Shown and Hidden from code or their default properties. One aspect of the use of forms to control processing is the use of Modal forms.
Modal forms are special in that the focus cannot be moved away from them until their processing is complete or they are closed or minimised. There are a number of built in forms such as Input Boxes and Message Boxes but any form can be made to exhibit Modality.
First, to demonstrate Modality we will include a message box in your application. Change the code in the Form2 dblclick event to read as below:-
Sub Form_DblClick ()
MsgBox "Form2 is now on top"
End Sub
When you run the application, clicking on the form has no further effect once the message box is displayed until the OK button is clicked and the message box closes. Message boxes can be made to react in many different ways and display an array of icons, see the VB3 help for further details.
We can stop the focus being placed on form2 once form1 is shown by making form1 modal. This could be done by altering the form2 click event to read:-
Sub Form_Click ()
form1.Show (1)
End Sub
the argument 1 alters the style of the form1 to modal. Start your application. You cannot re-activate any code on form2 until form1 is closed. This also displays a design problem:- distinguishing between click and dblclick events. This occurs because the click event occurs before the dblclick event. The problem (for our purposes) can be solved by assigning the required actions to the keyPress event, i.e.
Sub Form_KeyPress (KeyAscii As Integer)
MsgBox "Form2 is now on top"
End Sub
Run this to satisfy yourself that it works. Save the project as VB3aa3.mak, (where aa is your initials).
Visual Basic and all other Microsoft products mentioned
in this series are trademarks of the Microsoft Corporation.