Professional Documents
Culture Documents
Objectives
Notes
When you create a new project, VB provides a blank Form named Form1. You can create an
application with this Form, but for complex applications, you may need to work with multiple Forms.
For example, it is common to have a welcome screen and a main Form that branches to different
Forms. The high level structure of an interactive application can often be understood best from a
diagram showing the Forms hierarchy. You can have 3 Forms in your project: One Form might be the
opening Form, the other two for Addition and Multiplication routines. Just imagine how a Form
would look like if it has so many functionalities. It would probably look cluttered and confusing.
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:
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 Forms name. Do not confuse this with the Forms 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:
Form1
The Main
Menu
Form2
Science
Form4
Biology
Form3
Mathematics
Form5
Chemistry
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
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.
Sometimes you might want your application to start without any Form initially loaded. For example,
you might want to execute code that initializes a couple of variables before displaying any Form.
You can do this by creating a Sub Procedure called Main in a standard module, as in the following
example:
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.
2.
3.
Lesson in Action
Lets 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).
Set the StartUpPosition of frmMain to 2-CenterScreen. For the 2 other Forms, set this property to 1
CenterOwner.
On your Own
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 5 th 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.