Practice Exercise 2
Speed Converter

Objectives:  Gain experience writing a simple program in Visual Basic. 

Description:  This program is intended to provide you with experience using forms, text boxes, labels, and command buttons.  You will also learn to alter the values for various form and control properties.


In most foreign countries the speed limit signs display the limit in kilometers per hour.  Write a program that allows you to enter your the speed limit in kilometers per hour, converts it to miles per hour, and then displays the answer.

The interface should look as much as possible like the image below.  

Use a command button labeled "Convert" to actually start program execution.  In this program, the only action that should trigger an event is when the user clicks the Convert button.  Therefore the only event procedure that should contain code is the cmdConvert.Click event.  The following lines of code should all be placed between the Sub and End Sub of the cmdConvert.Click procedure.


Most programs follow the general structure:

Declarations:

Declare a local variable kph as type Single using the line
   
Dim kph As Single

Declare a local variable mph as type Single using the line
   
Dim mph As Single

Input:

Read kph from the text box using the line
   
kph = txtKPH.Text

The preceding line assumes that you named your first textbox
txtKPH Be sure to use the object naming standards for VB controls, and use names that are indicative of the use of the controls.  

Processing:

Convert the kilometers per hour value to miles per hour and store the resulting value in the mph variable using the statement     mph = Math.Round(kph * 0.6214, 0)

Output:

Finally, display the value in the label control using the line:
   
lblMPH.Text = mph

The preceding line assumes that you named your label control lblMPH.


When the program starts the cursor must be in the initial field, in this case the txtKPH field.  You can accomplish this by setting the control's TabIndex property to 0. (In all programs in this course you must be sure that the cursor automatically appears in the initial field, and moves to the correct subsequent field when the Tab key is pressed.)  All controls must be properly labeled (see example). 

Experiment with VB to figure out how to set the font for each control to Comic Sans MS, and font size to 12.  The border style property of the label field used to display the results (lblMPH) should be set to "Fixed Single" and both that control and the text box for user input (txtKPH) should have the alignment property set to "Right Justify."  The form caption should be set to display the caption "Speed Converter."

Be sure that your code is documented thoroughly.  In all programs you should include a comment block at the beginning or your program with your name, the assignment number, and a brief description of the purpose of the program.  You should embed comments in your code whenever the purpose of subsequent lines may be unclear to subsequent reviewers.  Comments begin with a single quote.


DEMO

Sample Solution

sln file