CIS 230, Visual Basic
Ch. 6 Notes, page 1
Objective #1: Create a project with multiple forms.
- Practically-speaking, you must use multiple forms in a VB project to create an
application that is non-trivial. While you could make extensive use of frames to group
related objects and be very creative with the Visible property, limiting yourself to one
form would make coding unnecessarily complicated. Creating a pleasing user interface
requires multiple forms in a project.
- You can create a new form (more formally known as a form module) simply by choosing the
menu command Project/Add Form and then selecting Form from the resulting window.
- You can add a preexisting form to a project by choosing the menu command Project/Add
Form and then selecting the Existing tab. This does not duplicate the added form which may
exist as part of another VB project. It simply links that form (.frm file) to your current
project. If the form is relocated in the future, the project will not be able to access
the form.
- You can add as many forms as you wish to a project but adding too many may make the
project unmanageable.
- The first form that is displayed or activated in a project is called the startup form.
Objective #2: Use the Show and Hide methods to display and hide forms.
- The Hide method of a form makes it disappear. For example, if the form
frmLevel2 were placed directly behind form frmLevel1, then the statement frmLevel1.Hide
would cause frmLevel1 to disappear and reveal frmLevel2.
- The Show method of a form makes it appear on the screen.
- The Show method can be set to 0 for modeless or 1 for modal.
(The VB constants VBModeless and VBModal also can be used as constant values.) For
example, the statement
frmLevel1 Show 1
displays frmLevel1 in a modal state. The default is modeless if the statement
frmLevel1 Show
is used without specifying a value.
- Displaying a form in modeless state allows the user to close that form or simply ignore
by making another form active.
- Displaying a form as modal, you require the user to respond to the form in some way.
This can be more effective than using a message box, especially since you can do much more
with a form than a simple message box.
- Two special events occur behind the scenes every time that a form first displays in a
project, the Form Load and Form Activate events, which
are not user generated.
- The Form_Load calls the form module into memory (RAM).
- A split second later the Form_Activate event occurs and actually activates the form,
passing control to it. If the form is hid with the Hide method, only the Form_Activate
method will be invoked when it is shown again. If there are tasks or initializing steps
that must be performed every time the form is shown, you should place those tasks and
statements in the form's Form_Activate procedure.
- If you no longer need to use a form throughout the remainder of the executing project,
you should conserve memory by unloading the form. This can be done with the statement
Unload frmLevel1
. Simply hiding a form does not clear the memory that it uses which could be significant
on older computers in projects that use many forms.
- While the Show method automatically loads a form, you can intentionally load a form
before you plan to use it in order to save time and make the form appear faster. This can
be done with the statement Load frmLevel1
- The Me keyword can be used anywhere in your code
that you would use the form name of the currently active form. If the form frmLevel1 was
the active form then the statement Unload Me would
clear the form from the memory.
Objective #3: Create procedures that are accessible from multiple form modules.
- When you create a new general procedure you must carefully determine whether it would be
something that is useful to a number of form modules or only one form module. If is is
only appropriate for one form module then you should place it in that form module and use
the keyword Private in its header. If you think that the procedure could
be used in numerous forms then you should place it in a standard code module and use the
keyword Public in its header.
- A standard code module (.bas file) is not associated with a particular
form but is present within the project. In fact, you should (but are not required to) name
the code module with the same name as the project itself (the extension will be .bas
instead of .vbp though).
- To add a code module, choose the menu command Project/Add Module and then select Module
on the New tab. Of course you can reuse standard code modules that belong to other
projects as well.
- Once you have created a code module, it will appear in the Project Explorer window.
- By placing general procedures within a code module you can easily make them accessible
to multiple form modules. Any procedure in a standard code module can be called by any
procedure in any module of the project. However, if you use the Public keyword in the
header of a procedure in a form module, then that procedure can also be called by
procedures in other modules.
Objective #4: Differentiate between variables that are global to a project and
those visible only to a form.
- Variables and constants can have three different levels of scope: local,
module level, and global. It is wise to keep the scope
of a variable as narrow as possible. That is, you should try to refrain from using module
level and global variables. You may have difficult to debug side effects
in your logic if you use globals.
- If you do wish a variable to be accessible to more than one form module, you must make
it a global. Simply use the keyword Public instead of Dim in its declaration statement.
However, it may confuse those who read your program if you "hide" global
variables in form modules in this way. You should always declare such global variables in
the general declarations section of the project's standard code module so that they are
easy to find and identify. You still have to use the keyword Public to make the variable
global even if it is declared in the general declarations section of the code module.
- You should use the the single letter prefixes of g and m within
identifiers (i.e. names) of global and module level variables respectively. Therefore, the
variable name for a global might be gcurSalary. Local variables do not need a prefix since
it is assumed that most of your variables will have local scope.
- Besides the aspect of a variable's scope, it is important to manage a
variable's lifetime as well. Local variables are only stored in memory
while a procedure is executing. As soon as the procedure ends, its local variables reset
to zero (or are actually abandoned by the computer's memory.) If you wish to keep track of
a running total for example, throughout the execution of a form, you should declare the
variable as Static. Simply use the keyword Static instead of Dim. For
example, Static
intRunningTotal As Integer declares intRunningTotal in such a
way.
- Read the Guidelines for Declaring Variables and Constants section on p.
216 very closely. You will be responsible for following those rules and conventions.
Objective #5: Create an About box using a form.
- Most Windows applications give the user the opportunity to see the name and version of
the software in an About box. Often "About..." is a menu
command under the Help menu. You can easily place such information on a form with labels
and add a menu option or command button that invites the user to see this info.
- VB also offers a template About Dialog form that can be found by choosing the menu
command Project/Add Form.
Objective #6: Add a splash screen to your project.
- A splash screen can be added to a VB project to make it look
professional as well. A splash screen loads as soon as a user executes a program. It often
makes it appear as if the application is loading faster than it really is.
- You can create your own splash screen or you can add a VB template found under the
Project/Add Form menu command. If you add your own, you should make sure that program
execution begins with the special sub procedure Main that is located in your .bas standard
code module.
Objective #7: Set the startup form or Sub Main to start
project execution.
- You can choose to have one of the forms in a VB project chosen as the startup
form. The startup form is simply the first form to load and execute. You can
select any form in your project as the startup form from the Project/Project Properties...
menu command. Under the general tab you can select the "startup object."
- You can also choose the Main sub procedure to be the startup object of a project. In
this case, any code within the Main sub procedure will execute
immediately. In this case if you wish to show a splash screen, you must show the splash
screen form with a command like frmSplash.Show
since that will activate the splash
screen.
CIS 230 Home Page | Mr. Minich's Education Home Page | Minich.com Web Design