Visual Basic Assignment #2
This page has been visited
times since March 8, 1999.
What new topics are covered in this assignment?
What properties are we going to use?
| Control | Properties we will use |
| Form | Caption |
| Label | Name, Alignment, Caption, Font, ForeColor |
| CommandButton | Name, Caption, Default |
| TextBox | Name, Text |
Notes:
Any information entered into a text box is considered to be a string. To read numeric values from a text box, convert them using the CInt and CDbl functions. For example, suppose "Text1" is a text box that contains an integer value. The Visual Basic statement "X = CInt(Text1)" reads the value from the text box and stores it in integer variable "X" for later use. If Text1 contains a real value, the command "X = CDbl(Text1)" reads the value and stores it in X as a real number. Variables in Visual Basic take the type of what is placed in them, unless they were previously declared with a dim statement. Thus the above statements will work correctly even if we have not declared the variables using dim.
To determine if the data in a text box is numeric use the IsNumeric function. It returns true if the value is numeric or false otherwise. This command is usually used within an if statement to change the course of the program depending on whether the data is numeric or not. Since we have not learned if statements yet, error checking of this kind is optional in this assignment.
Any numeric values that we want to
print in a label must be converted to a string first. For
example, to output the variable "X" to the label called
Label1 use the Visual Basic statement
Label1.Caption = CStr(X)
Strings can be added together when
printing to a label by using the plus sign. For example the
following statement is possible in Visual Basic.
Label1.Caption = "The value of X is " + CStr(X) +
" units"
One trick I used in this assignment was to clear all the output labels whenever either of the input text box values changed. I did this by putting the clear statements in the textbox.change event. Just double-click on the textbox to create the procedure and add the clear code.
To clear a label use the following:
Label1.Caption = ""
To clear a text box use the following: Text1 = ""
Math in Visual Basic is easy. The
following are examples of valid Visual Basic statements:
Y = X + 20
Y = X - 5
Y = X * 4
Y = X / 5
When printing real numbers in a label,
it is better to round them to the desired number of decimal
places first. For example, to round the equation "X /
5" to 2 decimal places use:
Y = Round(X / 5, 2)
To round x to 2 decimal places use Y = Round(X, 2)
Description of Assignment #2
In this assignment you will create a Visual Basic program that calculates miles per gallon from kilometers and liters. Again, rather than try to explain the program, I have decided to give you a copy of the .exe file. Just download the file below, unzip it in a folder on your computer, and double-click on them from My Computer or Explorer to run it. Your task is again to write a program that does what mine does. Yours may have a different user interface however.
Note that my programs do perform some error checking on the input values to make sure that they are numbers. This prevents my program from crashing when letters are entered where numbers are expected. This error checking is optional for this assignment. If you are interested in doing it in your assignment, it is done using an if statement and the IsNumeric command. Basically all I do is make sure that all required values are numeric before proceeding. If not, I skip around the code using the if statement. Contact me for more.
Please include some comments in your program. As a minimum for comments I would like to see your name, assignment number, and program purpose at the top of the main file. Also each procedure should have a short comment indicating its purpose. You can place comments anywhere in your program by first typing the ' character. Everything on that line that follows this character is considered to be a comment and is ignored by the compiler.
Again, when you are done the assignment place all required files in a .zip file and send it to me as an attachment.
If you have any problems with the above instructions, please e-mail me.
Below are the assignment 2 downloads:
Note: For those of you who do not have Visual Basic 6 on your computer, you will need to download the assignment #2 required .dll files to get necessary .dll files for this assignment. If you already downloaded this file for assignment #1, you do not need to download it again. Simply put assignment #2 in the same directory as these files.
Assignment #2 file (5,000 bytes)
Assignment #2 required .dll files (1.3 million bytes)
Exercise #2 (5,000 bytes)
Get more explanation on the Conversion Functions
To e-mail me click here
Page created and maintained by Ron Patterson