7/31/2014
Objectives
Chapter 6
Multiform Projects
McGraw-Hill
Include multiple forms in an application.
Use templates to create splash screens and About
boxes.
Use the Show , ShowDialog, and Hide methods to
display and hide forms.
Understand the various form events and select the
best procedure for your code.
Declare variables w ith the correct scope and
access level for multiform projects.
6-2
Copyr ight 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Creating New Forms
Using Multiple Forms
Projects can appear more professional when using
different windows for different types of information.
Often, the first form a project displays is a
summary form.
Projects can contain as many forms as desired.
Select Add Window s Form from the Project menu
and select from many installed templates.
6-3
Switching Between Forms (1 of 2)
6-4
Switching Between Forms (2 of 2)
In design time, there are several w ays to switch
betw een forms:
Selecting from drop-down
list
Solution Explorer window select a f orm name and
click the View Designer button or the View Code
button.
Double-clicking a f orm name opens the form in
designer.
Easiest way is to use the tabs at the top of the
Document window that appear when the f orm is
display ed
Each form is a separate file and a separate class.
Using Tabs
6-5
6-6
7/31/2014
Removing Forms from a Project
Adding Existing Form Files
Forms may be used in more than one projectan
existing form may be used in a new project.
Each form and information for the form is saved as
three separate files:
Code procedures
Visual interf ace
Property settings for the controls
To add an existing form to a project, use the Add
Existing Item command on the Project menu,
Select only one f ilename: FormName.vb; all three files
Select the file name in the Solution Explorer of the
file to be removed.
Either click the Delete key or right-click on the
filename to and choose Delete from the context
menu.
Additional option is to choose Exclude from project
to remove the form from the project but not delete
the files.
are automatically copied into the project folder,
6-7
6-8
An About Box (2 of 2)
An About Box (1of 2)
One popular type of form in a project is an About
Box found in most Window s programs under
Help/About
Usually provides the name and version of the
program and information about the programmer or
company
Users can create About Boxes by creating a new
form and entering the information in labels.
You can add Window s controls to a new About Box
form.
6-9
6-10
Setting Assembly Information
Using the About Box Template
Users can manually set the Text properties of the
controls (not recommended).
--OR- Open the Project Designer form.
Project/ProjectName Properties or double-click My
Project in the Solution Explorer
Visual Studios
About Box
template can be
used to create a
new About box.
Choose Add
Window s Form
from the Project
menu and select
About Box.
Click
the Assembly Information button and f ill in the
desired inf ormation in the Assembly Information dialog
box.
6-11
6-12
7/31/2014
A Splash Screen
Assembly Information Dialog Box
The initial screen containing a logo or w indow that
is seen w hile a program is loading
Professional applications use splash screens to tell
the user that the program is loading or starting.
Makes a large application appear to load and run
faster since something is displaying on the screen
w hile the rest of the application loads
6-13
6-14
Using the Splash Screen Template
Splash Screen Example
Visual Studio contains splash screen templates.
Select Project/Add New Item to display the Add
New Item dialog box and choose Splash Screen to
add the new form.
Modify the form to fit current needs.
A splash f orm
created using the
Splash Screen
template
6-15
Setting the Splash Screen Example
Making the Splash Form Display First
6-16
Display the Project Designer from the Project menu
and set the Splash screen drop-down list to the
created splash screen.
Do not change the setting for Startup object or
Shutdown mode.
When the project is run, the splash screen should
display w hile the startup formis loading, and then
disappear w hen the startup form activates.
Set the Splash
screen dropdow n list to the
new form in the
Project
Designer.
6-17
6-18
7/31/2014
Showing a Form
Modal versus Modeless Forms
New forms are displayed in response to a user
clicking a button or a menu item.
In the event procedure for the button or menu item
use either the Show method or Show Dialog
method to display the new form.
Show method displays a form as modeless means
that both forms are open and the user can navigate
from one form to the other
ShowDialog method displays a new form as modal
the user must respond to the form in some way, usually
by clicking a button.
No other program code can execute until the user
responds to and hides or closes the modal form.
With a modeless form, the user may switch to another
form in the project without responding to the form.
6-19
6-20
Show Method
ShowDialog Method
General Form
FormName.Show ()
Example
SummaryForm.Show ()
The Show method creates a form object from the
specified class and displays it modelessly. The
FormName is the name of the form to be
displayed.
General Form
FormName.ShowDialog ()
Example
SummaryForm.Show Dialog ()
Use the Show Dialog method when you want the user
to notice, respond to, and close the form before
proceeding with the application.
6-21
6-22
Hiding or Closing a Form
Hide Method
The Close method behaves differently for a
modeless form compared to a modal form.
Modeless Close destroys the form instance and
General Form
FormName.Hide()
remov es it from memory.
Modal the f orm is only hidden.
Choosing a forms Hide m ethod sets the forms
Visible property to False and keeps the form
instance in memory.
Example
SummaryForm.Hide()
An example would be f or a f orm with instructions or
Help text
6-23
6-24
7/31/2014
The Sequence of Form Events
Responding to Form Events
Two primary events that code might be needed for are:
FormName.Load f orm loaded into memory
FormName.Activ ated occurs when control is passed
to f orm
The first time a form is shown in an application, the form
generates both the Load and Activated events.
If a form is displayed multiple times, initializing steps
can be placed into the Activated event procedure; same
for setting the focus in a particular place on a new form
Load
Occurs before the form is displayed for the
first time usually happens only once
Activated
Occurs each time the form is shown
Paint
Occurs each time any portion of the form is
redrawn
Deactivate
Occurs when the form is no longer the active
form
FormClosing
Occurs as the form is about to close
FormClosed
Occurs after the form is closed
6-25
Writing Event Procedures
From the Properties Window in the Designer
Writing Event Procedures
From the Code Editor
6-26
In the Editor, drop down the Class Name list and choose the
entry that shows the events for the selected form.
In the Method Name list, select the event for which to write a
procedureev ents already having a written procedure
appear in bold.
Select an ev ent using the Properties window.
Click the f orm to show properties and click Events button.
Double-click the event to create an empty event procedure.
6-27
6-28
Variables/Constants in Multiform Projects
Holding the Splash Screen Display
If applications are very small, the splash screen
disappears before it can be read.
Code can be w ritten to hold the splash screen
longer by calling the:
System.Threading.Thread.Sleep() method in the
Load event procedure for the startup form of the
project.
Example:
System.Threading.Thread.Sleep(5000)
Note: this information
For module-level variables to be available in more
than one form in a project, it must be declared as
Friend or Public and not as Private.
Scope can be expanded for variable and constants
and is the set of statements that can access a
variable or constant w ithout qualifying its name.
Public is not recommended for multi-form access.
Use friend.
is not in the text, but can be useful.
6-29
6-30
7/31/2014
Lifetime
Access Level
Specifies the permission required to make use of the
variable or constant
To make a variable available to other forms, use either
the Public or Friend keywords.
Friend allows other f orms in the project to access the
The period of time that a variable or constant
remains in existence
Module and namespace variables exist as long as
the application runs.
v ariable
Public allows all other programs to access variables
(not recommended)
Private keyword allows access only in the class (form)
in which it is declared.
Only use access-level keywords for module-level
variables.
6-31
6-32
Static Variables
The Static Statement
Use to declare local and block-level variables
Retain their value for the life of the project
If the value in a variable needs to be retained for
multiple calls to a procedure such as running
count, declare it as Static.
If the variable is used in multiple procedures,
declare it at the module level.
General Form
Static Identifier As DataType
Examples
Static PersonCountInteger As Integer
Static ReportTotalDecimal As Decimal
6-33
6-34
Declaring Variables/Constants: Guidelines
Namespaces
VB projects are automatically assigned to a
namespace.
Namespaces can be viewed and modified, which is
called the root namespace.
6-35
Place all local declarations at the top of a procedure.
Use named constants for any value that doesnt change during
the program execution.
Keep the scope of variables and constants narrow.
Consider making variables local, if possible.
Make v ariables Static, if needed, for multiple executions within a
procedure.
If variables are needed for more than one procedure, declare it
as local and pass it as an argument.
Use Private module-level variables if using a variable in multiple
procedures and displaying in another.
If using the value of a variable in more than one form, declare it
as Friend.
6-36
7/31/2014
Running a Program Outside the IDE
The .exe file can be moved to another computer,
placed on the system desktop, or used as a
shortcut just like any other application.
If copying the .exe file to another system make
sure it has the correct version of the Microsoft.NET
Framew ork.
Can download the f ramework for free from the
Microsof t Web site
Change the icon, if desired.
6-37