Lab 9: Event
Handlers, Variables and Assignments
Spring 2001
Reading to be done prior
to Lab 9:
·
p. 69-75 (top of page)
in Chapter 4 of Computer Programming Fundamentals with Applications in
Visual Basic 6.0
Introduction:
In lecture this week the topics of variables and variable assignments were discussed. Today in lab you are going to build on Lab 8 and the introduction of the VB IDE. In Lab 8 you created a form with many objects on them. Those objects all have names associated with them. You will learn more about event handlers and how to attach code to certain event handlers to change the properties of those objects. You will also learn to declare variables, assign values to those variables, and use those variables as part of your program. If there is time at the end of lab, the concept of conditionals will be introduced.
Objectives:
TO DO:
To make the buttons do something we need to attach event procedures to
them. These are instructions that the controls execute when specific events are
fired. (Note: The command buttons, labels, etc. are all controls)
To attach an event procedure to each of our buttons we need to open the Code
Window and write some Visual Basic instructions.
Private Sub cmdBlue_Click()
First.BackColor = vbBlue
End Sub
***Remember that the VB IDE will help you out and type in the first and last lines that are needed for the event procedure***
Private Sub cmdRed_Click()
First.BackColor = vbRed
End Sub
· Open up the Code Window by double-clicking on the form, or by selecting View>Code from the menu bar.
· Select Form in the object list box and Load in the procedure list box (the Load event will come up by default). Type in the following code:
Private Sub Form_Load()
txtATextBox.Visible = False
End Sub
Private Sub cmdVisible_Click()
txtATextBox.Visible = True
End Sub
Declaring variables and assigning values:
So far we have worked with objects (controls), their properties and some assignments. Now it is time to declare variables and assign particular values to them.
Since we may want a little help in debugging our code in the future, were going
to ask VB to keep track of the variable names we use and alert us when they
haven’t been properly declared.
The next step in our program is to declare a variable (to allocate some space in memory) to hold a piece of data. The data that this variable will hold is a string of text that you will enter.
· Assign a value to the variable. We will have that value assigned when the form loads. (This is called initializing the variable)
·
Inside of your Form_Load() procedure, initialize your
variable creatorinfo by adding the statement:
creatorinfo = “Hello there! This is
<your name>, and I wrote the code for this program!”
· In the code window, select lblALabel from the object menu and the Click event from the procedure menu. The following statement should be added into the procedure:
lblALabel.Caption = creatorinfo
· Click on the Visible button and your text box appears.
· Type in some text in the text box
· Click on the “This is where you click!” button-your text in the text box now appears in the label.
· Now, click on the label. The value you assigned to creatorinfo appears as the caption of the label.
·
Trés cool, no?
· See if you can figure out how to insert the picture you ftp’d earlier into your picture box using the propertied of the object picMyPictureBox
· Once you add a picture, you always need to send the picture with the project and form files if you want it to appear. (Just like sending pictures to go with HTML!)
· However, if you make a program executable, then the image will be included in the code and you don’t have to worry about 3 separate files (.vbp, .frm, and the image file).
Looking ahead to Lab 10:
What happens when you want your program to make a decision between two possible actions a user might take? If the user takes one action (or enters certain information), a certain thing must happen. If they take another action, another, different thing must happen. When a choice must be made, then conditionals need to be used to give the program the precision it needs to run correctly.
Conditionals take the following form when used in a VB program:
If <some statement or this is true or false> Then
<perform some action>
Else
<perform a different action>
End If
The syntax (or the way this statement is written out) varies from language to language, but the idea is the same. The above syntax is what is used in Visual Basic.
For Lab 10, we will create a little program that asks a user to enter the temperature outside. Once they enter that information and click on a submit button, the program gives them a reply as to how cold or hot it is.
Think about how you would write an algorithm that would give one of two answers depending on the information entered by the user.
Your form (the interface) for Lab 10 will require the following objects:
2 labels
2 command buttons
1 text box
If you wish to get a head start on Lab 10, you can create a project that has a form with the following objects. Save it for use in lab next week.
See you next week!