SECTION 3 Input using a Scroll Bar

Visual Basic offers many controls that can be used to make apparently complex problems easy to solve with a simple solution. The Scroll Bar is one such tool that provides a sliding scale of numbers that can then be used to calculate another value. A simple example is shown below.

Problem

A program is needed to convert temperature in degrees celcius to degrees farenheit between 0 degrees C and 100 degrees C.

Output The temperature in degrees Farenheit
Input The temperature in degrees Celcius
Processes Convert the value for Celcius to Farenheit using rule F = C* 9 / 5 + 32

Form and Controls

Controls

This applications needs six Labels, one CommandButton and one Horizontal ScrollBar. The properties and settings for one of each control is shown below.

Object Property Setting
Horizontal Scroll Bar Name HScrDegreesC
  max 100
  min 0
     
Comamnd Button Caption End
  Name CmdEnd
     
Label Caption Temperature Converter
  Name LblHeading
  BackStyle 0-Transparent
  Font Ariel Black
  ForeColor (red)
     
Label Caption (blank)
  Name LblCelcius
  BackStyle 0-Transparent
  Font Ariel Black
  ForeColor Black

Pseudocode

GET Celcius Temperature (current Scroll Bar Value)
WRITE Celcius
SET Farenheit TO Celcius * 9 / 5 + 32
WRITE Farenheit

Code

Private Sub HScrDegreesC_Change()

' Read in the temperature from the scroll bar value and display in a label
LblCelcius.Text = HScrDegreesC.Value
'Calculate the Farenheight value and display the result in a label
LblFarenheit.Text = HScrDegreesC.Value * 9 / 5 + 32

End Sub

The above program will take the current value for the scroll bar and place it in the Label for the Celcius value. It will also apply the rule to that value and determine the Farenheit equivalent. This will be places in the Label for the Farenheit value.

As the scroll bar is moved, the labes for Celcuis and Farenheit are automatically updated.

EXERCISE 3

Choose ONE from the following and write a program with apprpriate documentation and comments to perform the task outlined utilising a scroll bar for input.

  1. The time taken to hear thunder after a flash of lightning can be used to calculate how far away the lightning strike was. Given that the speed of sound is 340 metres per second in air, write a program that will calculate how far away a lighning strike is if the time to hear thunder is given in seconds.
  2. Jan's Lollie Shop sells Smarties by the gram and each gram contains 6 Smarties. They also cost 10 cents a gram. Many children want to know how many Smarties they are getting. Write a program to show the number of Smarties and the price to be paid.
  3. Diagrams that come from the United States have measurements in inches. A program is needed to take measurements in inches and to convert them to centimeters. Note: 2.54 centimeters = 1 inch.
  4. If you want to buy goods on the internet, you often have to buy them in the currency of the country from where you are buying. The exchange rate is used to refer to the amount of money which one Australian dollar is worth in another currency and this continually changes. For example $A1 may be worth 74c US one day and due to economic changes 77c US several days later. Plan and write a program which will you to convert a price in some other currency to Australian dollars. The current rate of exchange will need to be entered.

Author: Mike Leishman

Last Modified 8 June 1998