Lab 8:
Event Handlers, Properties and Variable Assignment
Winter
2002
·
p. 69-75 (top of page)
in Chapter 4 of Computer Programming Fundamentals with Applications in
Visual Basic 6.0
Today in lab you are going to
build on Lab 7 and the introduction of the VB IDE.
In Lab 7 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.
Making the form do
something:
Although we have buttons on our form, we haven’t instructed them to do anything
yet. Try running your program. While
there are several ways to run a program from the IDE, for now just press F5 or
press the Start button on the toolbar. Click one of your buttons.
Does anything happen? No, because we haven’t given the program anything to
do. Press the stop button to end the
program and return to the design area of the form.
To make the buttons do something we need to attach code to event procedures.
These are instructions you write that are executed when specific events related
to controls are triggered. (Note: The command buttons, labels, etc. are all
controls)
To attach code to an event procedure for each of our buttons we need to open
the Code Window and write some Visual Basic instructions.
Private Sub cmdGreen_Click()
frmFirst.BackColor
= vbGreen
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***
While you are still in the Code Window notice that there are two drop down boxes at the top. The one on the left should say “cmdGreen”. This is the object list box. The one on the right should say “Click”. This is the procedure list box.
Private Sub cmdYellow_Click()
frmFirst.BackColor
= vbYellow
End Sub
Let’s work with one of the other buttons (cmdVisible) and the text box (txtATextBox) to demonstrate some other properties you can manipulate.
Private Sub Form_Load()
txtATextBox.Visible
= False
End Sub
Private Sub cmdVisible_Click()
txtATextBox.Visible
= True
End Sub
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.
When you use the Option Explicit statement, you must explicitly declare all variables. If you attempt to use an undeclared variable name, an error occurs. This will help you write better, more precise code.
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 value (i.e. text) that you will enter.
You only want the value of creatorinfo to appear in the caption of the lblALabel when someone clicks on the label. So the statement that controls that assignment should be located in the lblALabel click event.
lblALabel.Caption = studentInfo
What happens when you want your
program to make a decision between two possible actions? What if the program makes that decision
based on two or more actions a user might make?
If the user takes one action (or enters certain information), a certain thing
must happen. If they take another
action, then 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 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 9, we will create a little program that asks a user to give some information. Once they enter that information and click on a submit button, the program tells them something.
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 9 will require the following objects:
2 labels
2 command buttons
1 text box
12 option buttons (radio buttons)
If you wish to get a head start on Lab 9, you can create a project that has a form with those objects and experiment with them.