Welcome to Australian PC User Magazine Offline CD-ROM
PC User Web Site Games' Guide Education General & Business Applications Online Tools - All your Net Essentials Utilities Patches & Support Files PC User Interactive - Exclusive tutorials nineMSN
Software Contents

Home
Search
Help!

PC User Interactive

Quote of the day

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.

All the files required for this project are available on this month's cover CD-ROM. See the box 'How to use files on the cover CD' for more details.

This project is in two parts: the first part involves creating the file which will contain the quotes and provide the user with an interface for adding new quotes and editing the existing ones; the second part will be an interface for displaying a quote randomly chosen from the file each time the user starts up their computer.

The form which the user will use to manage the quotes is shown in Figure 1e. The code for this form creates the file in which the quotes are stored. This is a simple text file and, within it, each quote takes up a block of exactly 650 characters, which allows 600 characters for the quote itself and 50 characters for the quote's author's name, if this is known.

Each quote comprises the quote text and author details and is, in effect, a record in the file. As each quote is the same length (shorter quotes are padded out with spaces to a total of 650 characters) the program can randomly access the file and pick out, say, the fifth quote because it will begin 650 by 4 characters in from the beginning of the file. When you create a random access file, Visual Basic takes care of reading the records for you if you tell it the record number and how long each record is.

 

How to use the files on the cover CD
The files on the CD may not 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 managequotes.frm, managequotes.frx, modquotes.bas and quotes.vbx are also on the CD, as is the text of the code in code1.txt.

You will find all these files in the \interact\quoteday folder on the CD.

 

Creating the project
To create the form for this part of the project, start a new project and add the controls and set the properties listed in the table 'Controls and properties for form'.

The Add button will take up the same space on the form as the New button. Create the Add button first and set its properties, then create the New button the same size and place it on top of the Add button. Likewise, create the Exit button first and place the Quit button over the top of it. Because the visible property for both the Add and the Exit buttons is set to False neither of these buttons will show when the form is initially run.

Add a Module to your project using Project, Add Module, Module and type the code from Code Box #2 into the module and save it calling it modQuote.bas.

Type the text from Code Box #1 into the form and save the project. Call the form managequotes.frm and call the project quotes.vbp.

Run the program to test it. You should have a quote file with two initial quotes, which you can move through using the Next and Back buttons. You can also edit a quote by moving to the quote to edit and selecting the Edit button. To add a new quote select the New button.

While you're editing a quote or adding a new one, notice how the buttons on the screen change and only the Add and Exit buttons are visible and enabled. These buttons have replaced the New and Quit buttons and the Next and Back buttons are disabled.

When you have finished editing or adding a quote by selecting the Add button or the Exit button, you're returned to navigation mode and the buttons on the screen will be returned to their original states.

 

QUOTES1s.gif (2047 bytes)
Figure 1e: The basic form for this month's portion of the project allows the user to add and edit quotes.

 

note.gif (244 bytes)Controls and Properties Form

How it works
The code in the module creates a user-defined data type or record called typQuote. This type has two strings -- quoteText and quoteAuthor -- both of which are fixed length strings. These are needed where you're storing the data in a file which you will randomly read and write. If a quote or author takes up less than the allotted space, Visual Basic pads each string with spaces so that each record will be exactly the same size as every other record.

In the Declarations section of the form a number of variables are declared, including two which aid in navigation of the quotes.txt file -- currentQuote, which is the record number of the current record in the file, and lastQuote, which is the number of quotes in the file.

The form's load event contains the code to centre the form on the screen and to open the quotes file for random access. If the file does not exist, it will be created and two sample quotes will be added to it. The first quote in the file will be displayed in the text boxes on the screen by a call to the procedure readQuote, which appears in the module. This procedure reads the quote number it receives from the calling statement and displays it in the appropriate text box or label on the form that called the procedure. Next month's form will also use this procedure, so this is why it appears in the module and why the name of the calling form is passed to it.

The displayAlertMessage procedure also appears in the module, rather than in the form code, as it will be called by event procedures on both this form and the second form, which will be added next month. The procedure displayAlertMessage takes a message from the calling statement and displays it on the screen.

The procedure toggleButtons handles the changes required to the buttons on the form when the user selects Edit or New. When the Add and Exit buttons become visible, the New and Quit buttons must become invisible, the Next and Back buttons must become disabled and the two text boxes must be unlocked. The procedure toggleButtons both sets and resets various button and text box properties in one simple procedure. Those that were on are turned off and those that were off are turned on.

When the user selects New or Edit to add or change a quote the text boxes are unlocked so that changes can be made to them and, if the user chooses to add a new quote, the contents of the text boxes are cleared. The editFlag keeps track of whether the user chooses to add a new quote or alter one allowing the code in the cmdAdd_click event to be used in either instance. The text which displays in the label lblQuoteNumber is used at this point to give the user brief instructions.

When the user has completed their changes and they select the Add button, the text is checked to ensure that a quote has been typed (ie, the text is one character or more in length), that it is not longer than 600 characters and that the author's name is not more than 50 characters. If the text entered meets all these tests and it is a new quote, then the number of quotes is incremented and the currentQuote variable is made equal to the new total number of quotes. The quote is then written to the quotes.txt file.

If there is something wrong with the text entered, a message indicating the problem and how the user can go about fixing it is displayed.

The code for the navigation buttons Back and Next simply steps one quote at a time through the file, making sure that the user is not trying to proceed past the beginning or end of the file and alerting them to the problem if they try to do it.

 

Next month
In next month's column the form and code for displaying a randomly selected quote on the screen whenever the user starts their computer will be added and linked to this part of the project.

QUOTES2s.gif (1466 bytes)
Figure 2e: When adding or editing a quote the buttons on the screen change so only those relevant to the task are visible and enabled.

 

 

note.gif (244 bytes)Code Box 1

 

 

note.gif (244 bytes)Code Box 2

toppage.gif (1757 bytes)
All text ⌐ 1998 Australian Consolidated Press - PC User Magazine