The simplest computer programs are those which require only a number of separate sequenced steps. In this section you will plan and write computer programs for problems requiring a solution of this kind.
Consider writing a computer program for the following problem.
Wanneroo Turf Farm sells instant lawn for $19 per square metre and charges $45 for delivery. A program is required to compute the charge for customers purchasing lawn and having it delivered.
A Visual Basic solution is shown below. It is made up of a form onto which we can put controls. The form and controls have properties that can be set.

In the planning stage it is important to determine many things:
Output
The computer is going to show the final cost of buying the lawn.
Input
The computer needs to know the amount of lawn (in square meters) that is needed.
Processing
The computer needs to multiply the number of square meters of lawn by 19 and then add 45 to that answer.
Forms and Controls
By setting the properties of the form and each control, the screen above can be duplicated.
|
Property |
Setting |
| BackColor | &H00008080& (Green) |
| Caption | Wanneroo Turf Farm |
| Name | default (Form1) |
This applications needs six Label controls, one Image control (optional), one TextBox control and two CommandButton controls.
It is a good idea to give each control a meaningful and appropriate name. The following naming convention applies where Xxxx is the part of the name you give:
|
Control |
Name |
| Form | FrmXxxx |
| Label | LblXxxxx |
| Command | CmdXxxxx |
| Image | ImgXxxxx |
| Text | TxtXxxxx |
A complete guide to the naming convention can be found in books like Microsoft's Visual Basic 4 Step by Step.
A guide to settings control properties for the Wanneroo Turf Farm program
|
Object/Control |
Property |
Setting |
| Image | Name | ImgLogo |
| Picture | Metafile (the name of the file is not shown) |
| Label | Name | LblHeading |
| BackStyle | 0-Transparent | |
| Caption | Wanneroo Turf Farm |
| Font | Ariel Black | |
| ForeColor | (Blue) | |
| TextBox | Name | TxtAmount |
| Text | (blank) | |
| Font | Ariel Black |
| CommandButton | Name | CmdCalculate |
| Caption | &Calculate |
| Label | Name | LblAns |
| Caption | (blank) | |
| CommandButton | Name | CmdQuit |
| Caption | &End |
Think carefully! To calculate the cost, it is necessary to know the number of square metres of lawn purchased, lets call this TurfAmount. To calculate the cost (let us call this TurfCost), it is necessary to multiply TurfAmount by Price and add DeliveryCost. The result can then be displayed in a Label control.
Get Amount of lawn (TurfAmount) Set TurfCost To TurfAmount * Price + DeliveryCost Write TurfCost End
In the planning, it is important to decide on what constants and variables are needed to solve the problem. In this instance, the constant values are Price and DeliveryCost, and the variable values are TurfAmount and TurfCost. The names are descriptive and help to describe the process.
The following BASIC code has been created from the plan and is activated when
the calculate button is clicked on by the mouse. The results are shown in a
message box.
Private Sub CmdCalculate_Click() Const Price = 19 Const DeliveryCost = 45 Dim TurfAmount As Integer Dim TurfCost As Integer TurfAmount = TxtAmount.Text TurfCost = TurfAmount * Price + DeliveryCost LblAnswer.Text = "$" & TurfCost End Sub
When you look carefully at a BASIC program, you should be able to understand the way the computer will solve the problem. A program consists of a series of lines of code which contain the instructions that the computer follows to return the required answer.
Author: Mike Leishman Last Updated 8 June 1998
Edited by FDS for Visual Basic Express 2005 5/2/07