Lesson IV - Creating A Simple Program
In this lesson we shall create a simple program using Visual Basic's basic controls to covert centimeters into meters and inches. We will make use of some of the controls that you have learned in the previous lesson like the label, text and command button.

Creating The User Interface
We shall begin with a new form. First, close all copies of Visual Basic. Then start it up - you will see a new form (blank). Or you can just click File/New... on the menu bar and choose Standard EXE (VB5). Resize the form by dragging it's borders so that it is about a quarter of the screen size. Next click on the properties window and scroll to the Caption property. Replace 'Form 1' with 'Conversion Program'. Then, create a label on the form by clicking the label button on the toolbar and then dragging your mouse cursor on the form. Find the Caption property in the properties window and change it to 'Cm to be converted :'. Go to the Name property and change it to 'labFirst'. It is always good practice to name your controls with a prefix of the control type so that you don't get confused - in this case the control is a label. Therefore a 'lab' is placed before the name of the control. Resize the control so that the text fits in a single line. Next, create a text box that extends to the end of the form. Change it's Name property to 'txtInput' and it's Text property to '0'. This is where the user will enter data to be converted. Below these two controls, place a command button. Change it's Name to 'comConvert' and it's Caption to 'Covert'. This is the button that the user will click to carry out the conversion. Then, create two more labels and change their Names to 'labMeters' and 'labInches' respectively. Change their labels to 'Meters :' and 'Inches :'. Resize the labels and place one below the other on the left hand side. Create two more labels that extend to the end of the form and Name them 'labMconv' and 'labIconv'. Change both their Captions to '0'. Change their Alignment property to '1- Right Justify'. Last of all, create a command button and change it's Caption to 'Exit'. Change it's Name to 'comExit'. Your form should look like the form shown here.

Writing The Code
The next step is to write the code so that the program can function. First, double click the 'Convert' command button to access the code window (or you can click it once and go to View/Code). Modify the code so you see the following :

Private Sub comConvert_Click()
If txtInput.Text = "" Then
MsgBox ("Please enter a valid number")
Else
labMconv.Caption = txtInput.Text / 100
labIconv.Caption = txtInput.Text / 2.54
End If
End Sub

Then go back to the form window and double click the 'Exit' button. Insert the word 'end' between the the lines so that you see the following :

Private Sub comExit_Click()
End
End Sub

Once you have done that, your program is ready to run. Just press the Play button or hit "F5" on your keyboard to run the program. Experiment with the program by entering various numbers and even no numbers and see what happens after you click the 'Convert' button. Have fun.

Dissecting The Code
Let's see what the code does, step by step :

Private Sub comConvert_Click()
If txtInput.Text = "" Then
MsgBox ("Please enter a valid number")

Here the program checks the Text property of the txtInput text box. If it is blank ("") then a message box will appear with the message "Please enter a valid number".

Else
labMconv.Caption = txtInput.Text / 100
labIconv.Caption = txtInput.Text / 2.54

Otherwise, if the text box is not blank, the assigns the value of the text box's text property value divided by 100 (txtInput.Text / 100) to the Caption property of the 'labMconv' label (labMconv.Caption). The same applies for the next line except the 'txtInput.Text' value is divided by 2.54 instead.

End If
End Sub

Last of all the If loop is terminated by 'End If' and this section of the program is terminated by 'End Sub'.
The 'End' button code simply executes the 'end' command which ends the program. As with all coming lessons, you may download (2k) the file for this program.

I hope you have enjoyed this particular lesson. If there is anything you don't understand feel free to contact me. In the next lesson we shall learn more about the other controls available in VB (should be ready in a week or two).

Back to Learning Visual Basic.

If you have any comments or questions please write to CrydoVax (Varian Qua)
All graphics, html and text on this page is copyright of Varian Qua 1997

© 1997 varian@tm.net.my
Find out how you can contribute.
Vote for this page here.
Back to 3D Guru.
Back to 3D Studio.
Back to 3D Basix
Back to SiliconValley/Horizon.


This page hosted by GeoCities Get your own Free Home Page
1