All the files associated with this tutorial are
in the \INTERACT\VBQUIT folder on this
month's PC User Offline CD. The files main.frm, main.frx and
quit.vbx are on this months disc as is the text of the
code in quit.txt. These files won't all work in your
version of Visual Basic so you need to use the ones that
will.
Start by opening the
VBP file; if you get errors, then you can't use it. You
will, however, be able to copy the code from the TXT file
to save you typing it. Just make sure you have named all
your controls correctly so the code will match them.


Figure 1e: By creating a safer quit
option for your Visual Basic projects, users will be able
to confirm whether they want to quit the program or not.

Code
Box #1


Figure 2e: Looking up MsgBox Function in
the Help menu gives you a list of VB Constants, Values
and Descriptions.
|
Without a safer quit function in Visual
Basic, your application will close immediately when a
user selects the Close button or double-clicks on the
Control Menu Box (the program's icon on the titlebar). It
does this without any check to ensure that the user
really meant to do this. The function operates by
stopping the application from closing unless the user
actually chooses to by selecting OK when a message box
appears on the screen. Figure 1e shows the message box
open over the application main form.
To see how the safer quit works, start a new project
and add these controls and set these properties:
Object |
Property |
Setting |
Form1 |
Name |
Form1 |
|
Caption |
PC User Smarter Quit option |
Command |
Name |
cmdQuit |
|
Cancel |
True |
|
Caption |
&Quit |
To test the safer quit all you need is a single button
on a form. Type the text from the box 'Code for Form1'
and save the project. Call Form1 main.frm and call the
project quit.vbp.Run the program to test it. Try closing
the program by using the Quit button, double-clicking the
Control Menu Box, selecting the Close button and by
pressing Esc. Notice that the form won't close until you
actually elect to close it.
How it
works
The quit option works because it is placed in the
Form_Unload event, which is the event called just
before a form is actually unloaded. The code
places a message box, with two buttons OK and
Cancel, on the screen, asking the user whether
they want to quit or not.An IF statement
determines whether the user has selected OK or
Cancel. If they select Cancel then a value of 1
is assigned to the in-built variable called
Cancel. Any value for Cancel other than zero will
stop the form from closing so you can choose any
number here (except 0) -- we simply chose 1 for
convenience. If the user chooses OK, then the
form will close.
In both instances the IF statement is also
used to store text in the variables quitTitle and
quitMessage. This allows us to use a single
message box statement at the end of the procedure
whose message and title will depend on what
option the user chose. This makes for neater
coding than placing individual message box
statements inside the IF statement.
You may have noticed that the Esc key operates
as if you had selected Quit. This is because the
Cancel property of the cmdQuit button is set to
True. This results in any press of the Esc key
operating as if the cmdQuit button had been
selected. You can have only one control on a form
with its Cancel property set to True. Setting one
control's Cancel property to True automatically
sets to False the Cancel property of any other
control that was previously set to True.
It is a simple process to use this code in
your own projects.
|
Constants
and message boxes |
In the message
boxes in this month's project we have
used Constants rather than Values for the
buttons which appear on the message box. Compare these
two statements:
MsgBox
quitMessage, vbOKOnly, quitTitle
MsgBox
quitMessage, 0, quitTitle
They
both have the same result but one uses
the constant vbOKOnly and the other uses
the Value 0. When you're programming,
using a Constant rather than a Value
involves more typing, but it makes your
program easier to follow. This is
particularly so if you have to go back
and check or alter it later. You can use
more than one Constant by linking them
with a '+' symbol so that:
userAnswer
= MsgBox(quitMessage, vbOKCancel +
vbQuestion, quitTitle)
displays
the Warning Query icon and an OK and a
Cancel button on the form.
You can
also use VB Constants rather than Values
in IF statements, and, again these make
the program easier to follow. You'll find
a list of Constants, Values and their
Descriptions by looking up 'MsgBox
Function' in the Help (see Figure 2e).
|
|
|