Wyo VB - Ch. 8 Notes
Objective #1: Create a project with multiple forms.
- You can create a new form (also called form module) by choosing the menu
command Project/Add Form and then double-clicking Form on the resulting window.
You should always rename a form by changing its Name property as soon as you
add a form to a project. While it is conventional to name the first form in
a VB project frmMain, you can choose any appropriate name for a second form
as long as it begins with the prefix frm. I also highly recommend saving the
form with the File/Save Form As... menu command as soon as you've added it
to the project. This may prevent you from accidentally saving the form outside
of the folder that you created for your VB project.
- You will see that along with your frmMain, any new form that you have added
to a project will appear in the Project Explorer window. You can view a form's
interface by double-clicking its entry in the Project Explorer window. Each
form has a separate code window that can be viewed by first highlighting the
form and clicking the View Code button in the Project Explorer window.
- To remove a form from a project, you can right-click its entry in the Project
Explorer window and choose the Remove Form menu command on the shortcut menu.
This technique does not delete it from your computer's hard drive if it was
already saved.
- 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 also
use the Project/Add File... menu command to add a form to your project.
- You can add as many forms as you wish to a project but adding too many forms
may make the project unmanageable.
- You can add a splash screen to a VB project by choosing the Splash
Form entry from the window that appears when you use the Project/Add Form
menu command. This splash form is a template that is provided with VB. This
template is automatically named frmSplash.
- The first form that is displayed or activated in a project is called the
Startup Object of the project. Typically, your frmMain will be the Startup
Object of the project by default. You can make a splash form the Startup Object
by choosing the Project/Project Properties... menu command and selecting it
on the Startup Object combo box that appears on the General tab. This will
cause your project to first display the splash form. However, you will have
to use the Show method to make sure that your frmMain appears after the user
closes the splash form.
- You can create an MDI project with Visual Basic. An MDI (Multiple
Document Interface) project is one in which there is one parent form and several
child forms. The parent form must be created using the Project/Add MDI Form
menu command. A parent form has a gray background. You cannot place controls
such as labels, textboxes, and command buttons on a parent form. However,
a parent form can (and usually does) have a menu. Then add child forms to
the MDI project using the Project/Add Form menu command. However, to make
a form a child form, you must set its MDIChild property to True. This
causes the child form to appear inside of the parent form. The user can drag
and move the child form but only inside of the parent form. This is the interface
that you are already familiar with in MS Word where the application itself
opens one parent form and new Word documents are child forms. It is still
proper to include other forms in the MDI project that are not parent or child
forms. For example, it is conventional to use a normal, non-child form for
your About and Splash Screen forms.
- A project that does not contain a parent form is called an SDI project
(single document interface.)
Objective #2: Properly use the Show and Hide methods as well as the
Load and Unload events for forms.
- The Hide method of a form makes it disappear. However,
the form is still loaded in the memory (i.e. RAM) of the computer. So it can
be quickly displayed later in the program if necessary. 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.
- If there is no need to use a form later in the program, then you should
use the Unload event to unload the form from the computer's memory
by calling the Unload event as in:
Unload frmLevel1
- 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. Instead of the values 0 and 1, you can use the
VB constants VBModeless and VBModal.For example, the statements
frmLevel1.Show 1
or
frmLevel1.Show vbModal
display the form named frmLevel1 in a modal state. Since the default is
modeless, the statement
frmLevel1.Show
is equivalent to the statements
frmLevel1.Show 0
or
frmLevel1.Show vbModeless
- Displaying a form in modeless state allows the user to close that form
(with the X in the upper-right corner of the window) or simply ignore
it by clicking on an area of another form to make the other form active.
- Displaying a form as modal requires 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. In fact, it is better to place code
that you wish to use to initialize or set initial properties of various
objects in the Form_Activate event procedure rather than the Form_Load
event procedure.
- 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 that form from the memory of the computer.
- When the user clicks your project's File/Exit menu
command or whenever you want your project to end, you should use the following
For Each loop to be sure that all of your forms are unloaded. Otherwise,
it may appear to the user that your project has been exited but one or more
of its forms may still be loaded in the memory (RAM) of the computer. This
could cause the person's computer to crash due to lack of memory eventually
after he has opened other programs.
Dim frmTemp As Form
For Each frmTemp In Forms
Unload frmTemp
Next
End
In the code segment above, frmTemp is a local variable that could have any
name. It is being declared as a Form object (rather than an Integer, Single
or String variable). The For Each loop is a special kind of For loop that
cycles through all of the forms in your projects Forms collection. We won't
be studying collections in this course but behind the scenes VB keeps track
of all of the forms in a project by storing them in a Forms collection. By
the way, there are also collections called CommandButtons, Labels, etc.
Objective #3: Create procedures that are accessible from multiple form
modules.
- A standard code module is not associated with a particular
form but is included within the project.
- It is conventional to name your project's standard code module file with
the name of the project ( e.g. Ch8Proj1.bas ) or as stdCode.bas. A standard
code module uses a .bas file extension instead of .frm which is reserved
for VB forms. The Name property of your standard code module should be stdCode.
- To add a standard code module to your project, use the menu command Project/Add
Module and then double-click Module on the New tab. Of course you can reuse
standard code modules that belong to other projects as well by copying and
pasting it into other project folders.
- Once you have created a code module, it will appear in the Project Explorer
window. It can be accessed by double-clicking its entry there or by selecting
it and clicking the View Code button.
- A standard code module only has a code window. There is no form interface
associated with it.
- By placing general procedures and functions within a code module you can
easily make them accessible to multiple form modules. Any procedure in a standard
code module can be called from any form in your project. However, the Public
keyword must be used in the header of the procedure or function. Using the
Private keyword would only allow you to call the procedure or function from
within the standard code module itself.
- When you create a new general procedure or a function, 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.
- For example, here is a short standard code module that contains one public
general procedure and one public function:
Option Explicit
Public Sub Pause(intSeconds As Integer)
Dim sngCurrentTime As Single
sngCurrentTime = Timer
Do While (Timer < sngCurrentTime + intSeconds)
If (Timer = 0) Then '
if midnight
sngCurrentTime = 0
End If
Loop
End Sub
Public Function intGetRandom() As Integer
intGetRandom = Int(Rnd() * 11) + 0
End Function
Objective #4: Create an About form.
- Most Windows applications give the user the opportunity to see the name
and version of the software in an About form. 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 #5: 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 by giving the user something to read while the first form
is being loaded behind the scenes.
- 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 general procedure Main
that should be located in your standard code module.
Objective #6: 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."
- However, you should choose the Main general procedure to be the startup
object of a project. In this case, any code within the Main general
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
vbModal
since that will activate the splash screen.
- Your Main general procedure should look something like this:
Private Sub Main()
frmSplash.Show vbModal
Load frmParent
Load frmMain
End Sub
where the splash screen is being displayed with vbModal so that the user must
close it before continuing. While he is reading your splash screen, your parent
form and perhaps the first child form that needs to be shown within the parent
form is being loaded. Within the command button on the splash screen is the
actual code that would show the frmMain such ( frmMain.Show).
Objective #7: Use timer objects.
- A timer object can be added to a form by double-clicking the timer
control in the Toolbox. It looks like a stopwatch.
- A timer object has an Interval property which can be set to any whole
number between 0 and 65536. The Interval property measures how many milliseconds
occur between the automatic, repeated execution of the Timer event. For example,
an Interval property of 1000 would cause there to be a 1 second delay between
Timer events.
- A timer object has an Enabled property which can be set to either
True or False. If this property is True, then the timer object's Timer event
will execute.
- Code that is written in a timer object's Timer event will execute
at regular intervals defined by the timer object's Interval property.
- The proper prefix to be used in a timer object's Name property is tmr.
- A timer object can be used to cause animation.
- Here is an example Timer event procedure that would cause animation of a
shape object:
Private Sub tmrShape1_Timer()
If (shpShape1.Top < frmMain.ScaleTop) Then
shpShape1.Top = frmMain.ScaleHeight
End If
shpShape1.Top = shpShape1.Top - 1
End Sub
If the Interval property of the timer object tmrShape1 is set to the
value of 1 (i.e. 1 millisecond), this code would cause the shpShape1 object
to move very smoothly across the form. If the Interval property was set
to 100 or 1000 it would appear to "jump" in increments across
the screen.
Objective #8: Use control arrays.
- Remember that an array is a group of VB variables with
the same name. It is also possible to have an array of controls such as text
boxes. When you give more than one control with the same name, Visual Basic
gives you the option of creating a control array. Within a control
array, each individual control must have a different Index
property. The Index property is sometimes called the subscript of
the control.
- All of the controls in a control array share the same Click event procedure
(as well as some other events specific to that type of control.) Then a programmer
can take advantage of the Index parameter in the Click event procedure and
use it with an If/ElseIf/Else statement or a Select Case statement to make
the event procedure respond in different ways depending on which control in
the control array was clicked.
- To create a control array, you first place a control such as a command button
on the form using any technique. You should name the control immediately giving
it a name such as cmdClick. Then, you copy and paste that control to place
a second control on the form. VB will ask you if you wish to create a control
array. If you click yes, then the a control array is formed with the names
of the controls being cmdClick(0) and cmdClick(1).
Or, you first create a control such as a command button and give it a name
like cmdClick(0). You must actually type a zero in parentheses after the actual
name of the command button. This causes all subsequent command buttons that
are named cmdClick to automatically take the names cmdClick(1), cmdClick(2),
cmdClick(3), etc. The first control in a control array should always have
the Index property (i.e. subscript) of zero.
- Menu commands that have the same Name property form a menu control array.
Each command must have the same Name property but they likely will have different
captions. Each menu command in a menu control array responds to the same Click
event. However, the Index parameter in the header of the Click event procedure
can be used with a Select Case statement to efficiently program each of the
menu commands with separate actions.
- A control array is simply a group of VB controls that have the same name.
Remember, controls are objects that you can place on a form that such as command
buttons, text boxes, and labels. In a control array, each control must be
of the same class. That is, no matter how many controls are in a control array,
they all have to be the same type of control, command buttons for example.
You can not have both command buttons and text boxes in the same control array.
- Each of the controls in a control array can have unique properties though.
Therefore, you could make the BackColor of one command button vbGreen and
another's vbBlue but still expect the same Click event procedure to execute
when either one is clicked.