Chapter 1 Planning and Writing Visual Basic Programs

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.

Problem 1

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.

Planning

In the planning stage it is important to determine many things:

  1. the information the computer is going to give to the user (Output)
  2. the data that the computer needs to solve the problem (Input)
  3. the calculations which are required to obtain the cost of lawn
  4. the controls needed and the properties of the form and the controls

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.

Form

Property

Setting

BackColor &H00008080& (Green)
Caption Wanneroo Turf Farm
Name default (Form1)

Controls

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

Getting the computer to work it out.

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.

Pseudocode (this is just a series of statements in simple english which describe what the program needs to do).

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.

Code

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

A BASIC Program

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.

  1. Some words that are in the program are special and are recognised by the computer. These are Const and Dim.
  2. The Const tells the computer that there are some values that do not change throughout the program. It is not a good idea to just put the numbers in the main part of the program, especially if they might change in the future..
  3. The Dim tells the computer of the names of the variables and the type of data they represent. Variables are names given to values used by the computer. We must decide the name of the variable.
  4. After the value for TurfAmount has been stored, the assignment statement (TurfCost = TurfAmount * Price + DeliveryCost) causes the computer to perform the necessary calculation to find TurfCost.
  5. The "$" & TurfCost is put into a label. The $ symbol appears in the label because it is in quotes. The value of TurfCost is shown in the label because LawnCost was not contained within quotes. The & symbol instructs the computer to join the two parts of the message together.
  6. To signal the end of the program, the End statement is used. This is put into the End command button Mouse_Click event.

Exercise 1 (You must do ONE of these after each tutorial)

  1. Tony Rider's Curtains charge $28 per metre of material and a fixed charge of $105 when making curtains for customers. A program is to be created to perform the calculations for Tony Rider. First the number of metres of material is needed. The cost can then be calculated by multiplying this by 28 and adding 105. Complete a plan for this program to assist Tony Rider in the calculation of his customers' accounts. Use the Turf Program as a guide to create a Visual Basic program to perform this task.
  2. Metro Meat Works pays its employees a salary of $255 per week plus $25 per hour for every hour worked during the week. Design and write a Visual Basic program that will calculate the amount of pay owed to the employees of Metro Meat Works.
  3. Churchland Fencing Contractors charges customers $74 per metre for Trim-Lock fencing and $49 per metre to erect it. Churchland Fencing has contracted you to write a computer program using Visual Basic that will calculate the cost of supplying and installing a new fence. Design and write the program that could be used for this purpose.

Author: Mike Leishman Last Updated 8 June 1998

Edited by FDS for Visual Basic Express 2005 5/2/07