


|
 Quote of the day - Part 2
Helen Bradley shows you how to create
a program that displays a quote of the day on the screen whenever you start up your
computer.
|
Last
month's project created the quote file and the code for adding and editing the quotes.
This month's project creates the form you'll see when the quotes are displayed every day
and the mechanism for linking this to the form created last month (see Figure 1e). Begin by opening the project from last month and add a new form using
Project, Add Form, Form. Add the controls and set the properties on the new form listed in
the table 'Controls & properties'.
The remainder of the objects can be copied from the other
form. Select the form called frmManageQuotes and, holding down the Ctrl button while you
do it, select the two text boxes and the label and click the Copy button. Switch to this
new form and click Paste to paste these objects into the new form. Type the code from the
box 'Code box #1' into the form and then save it calling it frmDisplayQuotes.
As this new form is the form that you will want your user to
see when the program is run each day, you'll need to change the startup form. Select
Project, Project1 Properties, select the General tab, from the Startup Object list box
select frmDisplayQuotes and select OK. Save the project calling it quotes.vbp.
Run the program to test it. You should see the new form with
a quote randomly chosen from those in the file on the screen. Selecting the Close button
will exit the program. Selecting the Quotes button will load the form from last month's
project allowing you to add and edit your quotes file.
|
Final touches
To create a standalone version of the program, compile it using File, Make quotes.exe.
Select the drive and directory to save your file into and select Options and choose the
icon you want to use for the program's icon from the list and click OK and OK again to
compile the code into an executable file (see Figure 2e). When the executable file has been created you can add it to your
system's startup routine by right-clicking on the Start button on the Taskbar and select
Open. Select the Program group and then select the StartUp group and add a new shortcut
using File, New, Shortcut. Select the Browse button, locate the file quotes.exe on your
hard disk, select the file and click Open. Select Next and type a better name for the
shortcut than the filename, for example, 'Daily Quote', and select Finish. The icon for
your program should appear in the StartUp group window (see Figure 3e), and you can now
close the StartUp group window. The program will now run automatically whenever Windows is
started.
|
How it works
The code added to this form's form_load event is similar to that used in the other form.
It opens the file containing the quotes and calculates how many quotes there are in the
file. Then the program calculates a random number in the range 1 - x where x is the number
of quotes in the file and displays the quote at this position in the file in the text box
on the screen.Copying the text boxes and label
from one form to the other means that both forms contain these identical objects. Every
time the procedure readQuote is called it is passed the number of the quote to read and
the name of the form that called it -- me. The procedure displays the quote text and
author name on the form containing the code that called the procedure.
The form frmManageQuotes is opened as a modal form -- the
number 1 in the form show statement sets this state. This ensures that all input will
occur only on this form while it remains visible and loaded. Until it is either hidden or
unloaded you cannot access the form underneath.
|
Customise it
Once your project is running and tested you can consider some options for customising it,
such as:
- add an underlying graphic to the forms;
- create a more colourful interface;
- change the font and font size to display the quotes more
attractively;
- add an option to allow the user to edit a particular quote by
number; or
- add an option to allow the user to delete a quote. You could
do this by opening the quotes file, reading each quote from it one at time and in
sequence, except for the quote to be deleted, and then write each quote to a new temporary
file (temp.txt) before reading the next one. Close all the files, delete the file
quotes.txt and rename the file temp.txt calling it quotes.txt.
|
|


Figure 1e: This month's form adds the interface you
will see every time Windows loads displaying the day's quote.

Figure 2e: When you are
creating a standalone version of your program you can choose which form's icon will become
the program's icon.

Figure 3e: The completed
project with the quotes program loaded into Windows startup group.
How
to use the files on the cover CD |
The files on
the CD 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 that the code will match them.
The files frmdispl.frm, frmdispl.frx,
managequ.frm, managequ.frx, modquote.bas and quotes.vbx are on this month's disc as is the
text of the code in code2.txt.
You will find all these files in the \interact\quote2
folder on the CD.
|
|
|