Lesson XV. Working with Multiple Forms
Objectives
ü To create
applications consisting of more than one Form
ü To
include in applications Forms that have been created previously
ü
To provide simple code to enable users to navigate between
Forms at runtime
![]()
So how do we add a new Form to a project? You can do in 2 ways:
![]()
§
On
the Tool Bar, click the Add Form icon .
Click Form. This will display the following dialog box:
Form is the default selection and this is just what we need. Click Open. The newly added Form will be displayed and a new Form icon will be added in the Project Explorer.
§ You can also add another Form by doing a right-click on the Project Explorer, Click Add, then Click Form.
If you want to add an existing Form (in case your new project needs a Form you have previously created for another project), follow the same steps but instead:
§ Method 1: Select Existing Tab of the dialog box displayed above, locate the Form, select the Form, and click Open.
§ Method 2: Click Add File instead of Form. Locate the Form, select the Form, and click Open.
Note: The added Form will have a default name of Form1. Make sure that there are no other Forms currently in the project with this name. A loading error will occur if name duplicate is found. You may notice that the filename of this added Form still has its original filename. This is because VB keeps only one copy of each file (even if it is used in different applications). So if you want to use and modify an existing file, you must save it as another file with the Save As option by doing a right-click on the Form icon in the Project Explorer, click Save <theOrigFileName> As…, provide a new filename, select the directory where you would be saving the Form, and click Save.
You may also remove a Form from a project by selecting the Form on the Project Explorer, then clicking Remove <form_filename>.
Switching Forms at Runtime
It is virtually impossible to interact with 2 or more Forms at the same time. Only one Form can have the focus. To switch between Forms at run time, you need to hide (using the Hide method) the current Form (e.g. frmForm1) and show (using the Show method) the next one (e.g. frmForm2). This is done with the following statements. These statements should be attached to the event (e.g. button click) that causes the change of Form to occur.
frmForm1.Hide
frmForm2.Show
Note: frmForm1 is the Form’s name. Do not confuse this with the Form’s filename (the one with the .frm extension).
Every Form that is the screen displayed is first loaded into memory. Even when it is hidden it still remains loaded in memory. This is not a problem with two or three Forms, but if you have many Forms, you can run out of available memory. If this happens the solution is to unload some of the Forms the program no longer need. Use the Unload command to do this:
Unload frmForm1
Conversely, the Load command loads a Form into memory without displaying it. This is usually unnecessary since VB performs a load automatically if the Form to be displayed is not yet in memory.
You can also show a Form without hiding the current one. In this case, you will have several Forms on top of the other – the top most is the active one. You may not want this to happen if do not want the user to interact with another Form without unloading or disabling the current one. One situation is when you have a hierarchy of Forms:

The Form1 displays the main menu that branches out to Science and
Mathematics. Once the user
clicks on the button that leads to Science, you may want to disable Form1
until the user closes Form2. In
this case, there is only one way of going to Mathematics: through Form1.
We do this by disabling a Form through the Enable property before showing another Form as in the following statements:
frmMainMenu.Enable = False
frmScience.Show
Somehow, the user is able to close frmScience and focus is given back to frmMainMenu. We do this by unloading frmScience, enabling frmMainMenu and giving back the focus to it (note that enabling a Form does not give the Form the focus). The code will be:
Unload
FrmScience
frmMainMenu.Enable
= True
frmMainMenu.SetFocus ‘Set the focus to
frmMainMenu
There is
an easier way of doing this. The procedure
below calls frmScience with two arguments: vbModal
and Me. vbModal tells VB
that the current Form should be disabled until the called Form is
unloaded. Me means that the
calling Form is the owner of the called Form, and that when the
latter is unloaded, control is passed to the owner Form.
Private
Sub cmdGoToScience_Click()
Call frmScience.Show(vbModal,
Me)
Sub Main( )
intStatus = “Locked” 'Show a startup form frmMain.Show This procedure must be a Sub procedure, and it cannot be in a Form Module. To set the Sub Main procedure as the startup object: 1. From the Project menu, choose Project Properties.
2. Select the General tab
3. Select Sub Main from the Startup Object box.
Let’s create an application with 3 Forms. The first Form (frmMain), as shown below, is the Main Menu. The button labeled Mathematics loads the Mathematics Form (frmMath). The button labeled Science loads the Form (frmScience). Below are the screenshots of the Science and Math Forms. Set the StartUpPosition of frmMain to 2-CenterScreen. For the 2 other Forms, set this property to 1 – CenterOwner. Write the following procedures for frmMain.Private Sub cmdMath_Click()
frmMath.Show vbModal, MeEnd Sub Private Sub cmdScience_Click()
frmScience.Show vbModal, MeEnd Sub Private Sub cmdQuit_Click()
EndEnd Sub
Write the following procedure for frmScience and frmMath.Private Sub cmdBack_Click()
Unload MeEnd Sub
1. What happens when you attempt to hide an unloaded Form?
2. Create a multiple Form application. Each Form has a button labeled Previous and Next. By pressing the Next button, a Form is displayed one after the other (Form1, Form2, .., Form5). When the 5th Form is loaded, the Next button loads the 1st Form. The previous button loads the previous Form e.g. if the active Form is the 3rd one, then load the 2nd one. Note: Only one Form should be displayed, so unload the active Form before loading another.
3. Modify the application you created in item 2. In a Label on each Form, display how many items each Form has been accessed (or loaded).
4. Create a simple login functionality. Add 2 Forms to a project. The first Form asks for a password in a TextBox. Assume that the password is “Letmein” with the quotes. When the user enters this password, unload this Form and load the other Form. Otherwise, prompt the user of the incorrect password using a MsgBox.
[ Home ]