CSCI 220
Programming With Visual Basic
Lecture 9: Modules, Multiple forms
Modules
Separate files on disk
Like the code part of a form. No objects attached.
Are saved on disk with a .BAS extension
Contain
Record definitions (see below)
GLOBAL variable declarations
Global procedures
Used for
Declaration of Global variables
Use GLOBAL keyword in place of DIM
Can be used only in general area of a module
Otherwise used just like DIM
Creation of global procedures
Can be called from any form, any procedure in
project
just like a new command
Can be used to store library of useful subprograms (EG
Printing related)
Created by choosing New Module from File menu or hitting
New Module button.
Scope Map:
General procedures
Created in general area of forms or modules
In forms, makes a form - level procedure
in module, makes a global procedure
Can be called from within code - New command
Syntax: Sub ProcedureName (Parameters)
Creates a new section of the general area
Code can be written here
Parameters
"Instant" local variable(s)
Don't have to be declared (!)
Calling a procedure - Simply use its name in code.
Multiple forms
Creation: New Form on File menu or New form button
Useful Methods
Show
Syntax: FormName.Show [modal]
FormName is the name of a form you have created in
the project
Example: Second.Show 1
Loads Second into memory if it's not there, shows
it to user
Modal switch
If you end the show method with a 1, the form will
be modal
New form gets focus
Next code activated is in the form_load
procedure of the second form
Control reverts to original form after second
is closed
You usually will want to do this
If you do not put a 1 at the end,
New form shows up, does not get the focus
control of program moves on to next line of
code in first form
Hide
Syntax: FormName.Hide
Example: Second.Hide
Hides the named form.
Control reverts back to procedure that showed
the form
Warning! Don't hide ALL the forms in a project!
Startup form
One form must be designated as startup form
By default this will be the first one created
To change, use the Project dialog under the Options
menu
Your Project- The quiz maker
(VB9)
Make a form that looks something like this:
Create a Module
Declare a new record type called QuestType. QuestType
should have the following fields:
Question (string, 50 characters)
AnsA (string, 50 characters)
AnsB (string, 50 characters)
AnsC (string, 50 characters)
Correct (string, 1 character)
Create a Global QuestType array called Quest. It should
have three elements (1-3)
Go to the form. In the general area, create an integer
variable called QuestNum
Form_Load
Initialize the Quest array. Example:
Quest(1).Question = "What is your name?"
Quest(1).AnsA = "Arthur, King of the Britons
Quest(1).AnsB = "Roger the shrubber"
Quest(1).AnsC = "Galahad the Chaste"
Quest(1).Correct = "A"
Initialize the value of QuestNum to one
Call the question writing subprogram (that doesn't yet
exist)
In each label, call the CheckAns procedure, giving it an
"A", "B", or "C" parameter
WriteQuest
Using the current value of QuestNum to choose the array's
index, fill the labels with the appropriate values from
the Quest array
Put this code in a general procedure of the form
CheckAns
Create a general-level procedure called CheckAns
Give it a parameter called guess
If the user's guess is the same as the current value of
the Correct field of Quest, then
increment the question number
If new Question number is less than 4 Call the
WriteQuest procedure
If the user was wrong,
Send a messagebox telling the user so.
After all three questions are answered completely, call a
modal form that will congratulate the user
End the program.
Have Fun!
Return to Syllabus