Visual Basic Lab Exercises
Visual Basic Lab Exercises
You will be asked to save both the form and the project. Make sure you save them on your
server. Give them the same name: ct3200lab1
In your Properties Window, change the caption of the form to “Lab One”
Go to the bottom of your Properties Window and change the WindowState Property
from “0-Normal” to “2-Maximized”. This will make sure that your project runs, it will take
up the full screen
Click on the Command Button in your toolbox. If you click it once, you will get a set of
crosshairs that tell you that you can draw a button the size you want. If you double-click
the command button in your toolbox, a button will appear on your form.
After you have created your button, you will need to change the caption. Click on the
button so that a set of squares appears around it. This will tell you that it is selected.
Then, go to your Properties Window, find the Caption property and change it to “Click
Me”
Now go back to the toolbox and create 2 textboxes in the same way you created the
command button. You will notice that inside each textbox will say “Text1” or “Text2”.
You will have to delete these words by clicking on each textbox, then going to the
Properties Window and finding the “Text” property. Erase “Text1” from the box, then
click on the second textbox and do the same thing.
Next, you will add a label to your form. Find it in the toolbox and add it to the form. Make
sure it is selected, then go to the Properties Window and change the following
properties:
Double click on the command button. Your screen will change and this will appear:
End Sub
You are now in the code view mode. Here is where you will start doing some programming.
You are going to make a sentence appear in the big label by entering some information in the
textboxes and clicking on the button.
Start by typing this into the space between "Private Sub" and "End Sub":
Label1.Caption =
This tells the program that when the button is clicked, something is going to appear in
the label.
Now add this to the line
Now we are going to make whatever we type in the textboxes appear in the label. Add
this to the line of code:
This is saying that the text in each textbox will be added to the contents of the label.
You need to add the plus signs so that everything will appear in order in the label.
The set of empty quotation marks between Text1.Text and Text2.Text is there so there
will be a space between the first name and last name when the program runs.
End Sub
Now run your program. Click the triangular Play button on the top of your screen.
Type your first name into the first textbox, then your last name into the second text box.
Then click the button.
Your screen should look something like this:
Notice that there is no space between 'is' and your first name.
To stop the program, use the 'X' in the corner.
Go back into your code and add a space between 'is' and the quotation mark. Then run
the program again. Type in the first and last name and click the button. The space
should be there now.
If you have time left, it would be nice to have a button on the screen to end the program
so we don't have to use the 'X'.
Go back to your form and add a button. Change the caption to 'Exit', then double-click
on it to enter the code view.
In the space between the existing code, add this line:
End
Now run your program. When you're finished, click the Exit button.
Finally, let's add a button to clear the textboxes and the label, in case we make a
mistake when we enter our inof into the textboxes.
On your form, create a button. Change the caption to 'Clear', then double click on it to
enter the code view.
In the space between the existing code, add these lines:
Text1 = ""
Text2 = ""
Label1.Caption = ""
What the set of empty quotes says is that we want nothing to be in the textboxes or the
label.
Now run the program again, enter the info in the textboxes, click the button to fill the
label, then click the Clear button.
Situation: The canteen has decided to go hi-tech and create a small cash register to add
up customer's totals.
To make this work in VB, you have to be able to take a number from a textbox and
perform a calculation on it. To do so, you need to add something to the textbox code
you created in Lab #1:
Step 4: Now you need to add all your variables together to get the
total for the customer:
total = chips + ..... + ..... + ..... + .....
Step 5: Now you have to display your total to the screen. Make sure
you choose the right label.
Label8.Caption = ? (What goes in place of the ?)
Step 7: Double-click the "Clear All" button. Write the code to clear all
the textboxes and the label for displaying the total. Think about how
you did it for Lab #1. Then run your program to see if it works.
Step 8: Double-click the "Exit" button and write the code to make it
work. Run your program to make sure it works.
Extras
Disable the "Clear All" button when the program first starts and
enable it again when the "Calculate Total" button is clicked.
Why do you do this? Well, in order for your entire program to see
and use the variables you will use for this lab, you need
to declare them first. Think of it as being like putting a sign on the
bathroom door to tell visitors that it is there for everybody to use.
STEP 3: Go back out to your form and double-click on the 'Student
#1 Avg' button to bring up the code view. Type in this line of code:
Private Sub Command4_Click() NOTE: Your buttons and labels might have different
names
Call s1
Label9.Caption = student1
End Sub
'Call s1' means that you are going to call a function named 's1'
that will do a calculation for you.
STEP 4: Move down a couple of lines past this code and type in
the following:
Private Function s1()
student1 = NOTE: 'student1' is a variable that will hold the first student's average until you
display it or use it in another calculation
End Function
The equation after 'student1=' has been left out because I want
you to figure out how you would calculate an average. What do
you do? Add up all the marks and divide by the number of marks.
Remember the code you wrote for Lab #2? You need to think
about that in order to fill out the equation you need.
STEP 5: Set up your button for Student #2:
Private Sub Command4_Click() NOTE: Your buttons and labels might have different
names
Call s2
Label10.Caption = student2
End Sub
STEP 9: Now you need to set up the button to calculate the class
average:
Private Sub Command3_Click()
Call class
End Sub
STEP 10: Now you need the function that will calculate the class
average for you:
Private Function class()
classavg = (student1 + student2 + student3) / 3
End Function
STEP 11: Write the code for the 'Next Student' (Clear) button and
the Exit button.
Step 12: Run your program. Enter some numbers in the textboxes,
then click the first student average button. Click the Next Student
button and repeat the process for the second and third students.
Then click the Class Average button.
If everything doesn't work, don't worry about it. IT WILL WORK
EVENTUALLY!!
Extras
Disable the 'Class Average' button until it is needed (until the
3 student averages have been found)
Visual Basic Lab #4
End Sub
STEP #4: Go out to your form, double click the Clear button and
type this line of code:
Private Sub cmdClear_Click()
picOutput.Cls
End Sub
Step #5: Run your code to see if it works. Save your project and
form as 'ct3200lab4'
STEP #2: Go into your code window and add this code on the blank
page:
Dim divisor As Single
Dim dividend As Single
Dim quotient As Single
Dim remainder As Single
If you declare a variable as 'Single', it means that the value it holds can
range from as low as 1.4 x 10-45 to as high as 3.4 x 1038
STEP #3: Now add this code under the cmdDivide button (go back to
your form and double click on the Divide button if you need to)
Private Sub cmdDivide_Click()
STEP #2: Open your code window and enter the following code to
declare the variables you will be using:
STEP #3: Enter the code for the button that will perform the
calculation
NOTE: Here are some things for you to remember that will help the
TOTAL INCHES equation make more sense:
1 mile = 63360 inches
1 yard = 36 inches
1 foot = 12 inches
**Only type in the code below that is in red
** Your command button might not be called 'cmdDisplay', or your
picture box might not be called 'picResult'. CHANGE THE CODE
BELOW TO MAKE SURE YOUR NAMES MATCH.
NOTES:
FormatNumber (kilometers, 1) takes the value in the variable
'kilometers' and rounds it to 1 decimal place.
picResult.Print by itself inserts a blank line to help space your
text
STEP #4: Enter the code for the Clear button. You will need to clear
out the 4 textboxes and the picture box
HINT: Think back to your last labs to figure out how to clear the
textboxes and picture box.
STEP #5: Run your program and put some numbers in the
textboxes to see what happens.
STEP #2: Now you will write the code for the 'Compute Transaction'
button that will do most of the work for this program. Double-click the
'Compute Transaction' button and type in the following code (only type
what you see in red):
STEP #2: Make sure you have the timer selected on your form. In the
properties window on the right, set the Enabled property to False and
the Intervalproperty to 100.
STEP #3: Enter this code for the cmdStart button (only the code in red):
STEP #4: Enter this code for the cmdStop button (only the code in red):
STEP #5: Enter this code for the cmdReset button (only the code
in red):
STEP #6: Enter this code for tmrWatch timer (only the code in red):
STEP #7: Enter the code for the cmdExit button (you should know this
code by now):
Private Sub cmdExit_Click()
WHAT GOES HERE?
End Sub
STEP #8: Run your program to see if it works
Extras
In the Properties window, change the Interval to 10. Then in your
code, change the code for the timer from 0.1 to 0.01. What
happens?
STEP #3: Double click on the cmdRates button and enter the
following code to make this button work (type only the code in
red):
STEP #4: Double click on the cmdBill button and enter the
following code to make this button work (type only the code in
red):
STEP #5: Enter this code for the sub procedure that will calculate
the total bill and display it to picBill: (type only the code in red):
STEP #6: Enter the code to get the Clear button working. You need
to clear both picture boxes and both textboxes.
STEP #7: Enter the code to get the Exit button working.
STEP #8: Run your program and try different combinations of
items and time.
STEP #9: Save your program as ct3200lab14
NOTE: The first time this loop executes, it will send a 1 to f1.
Then, it moves to the inside loop and f2 runs through all the
values of f2from 1 to 10. So when you run the program and
look at the screen, your top row of the table will show the "1
times" facts. After that, it runs through the loop again,
changes f1 from 1 to 2, and so on. On the click of the
button, 50 calculations are made.
STEP #3: Enter the code to get the Clear and Exit buttons working
STEP #4: Save your work as 'ct3200lab17'
STEP #5: Run your program. Click the "Display Multiplication
Table" button. If you entered your code correctly, you should
display a chart with all the multiplication facts up to 10 x 10.