Lab 4
Lab 4
LAB 4
TABLE OF CONTENTS
Objectives ..................................................................................................................................................... 2
Introduction .................................................................................................................................................. 2
Activity 1: Gate Takings Application ..............................................................................................................2
Activity 2: More on Variables ........................................................................................................................ 5
1
Objectives
At the end of this lab activity you should be able to:
Design forms.
Program with variables.
Use the Convert class methods or procedures to convert data from one data type to another.
Introduction
In this lab, you will become acquainted declaring and using variables in simple Visual Basic Windows
applications.
The Olympic Games Organizing committee wants an application that they can use at the end of the day to add
their cash takings from the sale of tickets from the various games that are being played on a particular day. The
application should also be able to sum up the number of spectators from the various games.
1. Declare a constant variable called dblTICKET_PRICE of type Double to store ticket price.
Assign it any value.
2. Declare a variable of type integer called intSpectators to store number of spectators.
3. Declare a variable of type double to store gate takings called dblGateTakings.
4. Use the Convert class method .ToInt32() to convert the number of spectators entered in the
textbox to an Integer value and store it in the variable declared in step 2.
intSpectators = Convert.ToInt32(txtSpectator.Text)
5. Calculate gate takings by multiplying ticket price (declared in step 1) by the number of spectators
and store it in the variable declared in step 3.
lblCashTakings.Text = Convert.ToString(dblGateTakings)
e) A command button or simply a button called “Add” to capture the cash taking displayed in the label and
the number of spectators entered to the running total.
2
The following hints may be used:
3. And then clear txtSport, txtSpectator and lblCashTakings and set focus to
txtSport.
f) A label to display the grand total of the cash takings (lblTotal_CashTakings ) and another label
(lblTotal_Spectators ) to display the grand total of the number of spectators at the games.
g) A command button called “Display Totals” to show the grand totals of the cash takings and the
number of spectators in the labels.
lblTotal_CashTakings.Text = mdblTotal_GateTakings.ToString("C")
lblTotal_Spectators.Text = Convert.ToString(mintTotal_Spectators)
Me.Close()
2. Application Behaviour:
i. User enters details of a particular sport – name and number of spectators for that sport.
ii. Hits the “Calculate” button to reveal the gate takings for that sport.
iii. Then hits the “Add” button to accumulate the gate takings and number of spectators to running total
variables for gate takings and number of spectators respectively.
iv. Repeats steps (i) to (iii) for as many sports as you like.
v. Click the “Display Totals” button to reveal the day’s gate takings and total spectators from all the sports
that were entered.
3
3. Pre-Lab (before coming to the lab)
Design the form for Activity 1. You should draw a sketch of the user interface and all the controls on the
form. Think of the variables and their type that you are going to use. Decide on the scope you need to give to
the variables (block, procedure or class/module level).
Add a label control in which you can display all the sports that were played on that particular day. Remember
the user types in the name of the sport, e.g. Golf, Lawn Bowling, Tennis etc. You will need to concatenate the
sport names entered in a string variable so that when the user clicks “Display Totals”, the text property of
the label reads the names of the sports that were entered e.g. “Golf, Lawn, Bowling, Tennis,”. i.e.
Assign the value of the string variable to the text property of the label. The concatenation can be done each
time the “Add” button is clicked. This string variable needs to have class or module level scope. For example:
4
Activity 2: More on Variables
Your friend, a science teacher, ask you to write an application that will allow his students to find the volume
of a cylinder by entering certain measurements such as the radius and height of the cylinder. The application
should have the following elements:
Your program should have a constant variable to hold the value for the PI. Set its value to 3.14159.
1. In addition to solving this problem, improve the interface by adding appropriate Access Keys to the
command buttons, setting the TabIndex and designate a default and cancel button.
2. Show the finished program to your tutor if there is one.
Modify your application so that it also calculates the surface area of the cylinder, with one circular end
open.
5
Pre-Lab (before coming to the lab)
Design the form for Activity 2.