FIT 100

Lab 9: 

Event Handlers, Properties and Variable Assignment

Spring 2002

1.       Retrieve the work from last lab. 1

2.       Adding Instructions to Event Handlers 2

3.       Declaring variables and assigning values 3

4.       Creating an Executable File. 5

5.       Looking ahead to Lab 10. 6

6.       Lab Notebook Questions 7

 

Reading for Lab 9:

·         p. 69-75 (top of page) in Chapter 4 of Computer Programming Fundamentals with Applications in Visual Basic 6.0

OR

·         Chapter 3: Performing Operations and Storing the results from The Visual Basic Coach.  This is on e-Reserve at the library:

https://eres.lib.washington.edu/coursepage.asp?cid=1113&page=01

 

 

Introduction:

In lecture this week the topics of variables and variable assignments were discussed.  Today in lab you are going to build on the introduction of the VB IDE. 

Last lab you created a form with many objects on them.  Those objects all have names associated with them.  Today, you’ll learn more about event handlers and how to attach code to certain event handlers to change the properties of objects.  You’ll 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:

 

1.    Retrieve the work from last lab 

 

  1. Download the Lab8_yourname folder that contains the project file with your name and the frmFirst form file to the desktop.  Or, if you saved it to disk, go to the project file there.

    (Note:  You named the project file with yourname.vbp and the form file frmFirst.frm).  

  2. Double click on the vbp file with your name to open up the project and the correct form.

Event Handlers

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.

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


2.    Adding Instructions to Event Handlers

 

  1. Open the Code Window by double clicking on the cmdGreen button. You are inside of the Click() event procedure. Enter the following code so that your procedure looks like this:

 

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***

 

  1.  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 “cmdBlue”. This is the object list box. The one on the right should say “Click”. This is the procedure[event] list box. Select cmdYellow from the object list box and type in the following code so that your procedure looks like this:

 

Private Sub cmdYellow_Click()

frmFirst.BackColor = vbYellow

End Sub

 

  1. Close the Code Window and run your program. [Run is equivalent to “playing” the program.  Find the play icon on the top of the page]

    What happens when you click on the Yellow or Green buttons?  Turn to the person next to you and explain it to them.

 

  1. Stop the program so that you can continue with the next steps.  Look for the appropriate icon or menu command what will stop the “playing” of the program.

 

  1. Work with one of the other buttons (cmdVisible) and the text box (txtATextBox) to demonstrate some other properties you can manipulate.

·         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 

 

  1. Select cmdVisible in the object menu and Click in the procedure menu and type in the following code:

 

Private Sub cmdVisible_Click()

txtATextBox.Visible = True

End Sub

 

  1. Run your program.  What happens when the form loads?  What happens when you click the Visible button?

  2. Stop running the program.

  3. Save your project.  You have set the names of the form and the project, so all you need to do is click on the save icon to save both.

  4. Inside of the cmdClick button click event, assign the value of the text in txtATextBox to the caption of lblALabel.  Add this statement in between the opening and closing statements that appear:

    lblALabel.Caption = txtATextBox.Text

  5. Run your program.  Click on the Visible button and your text box appears.  What happens when you then click on the “This is where you click!” Button?

 

  1. Save your project again.

 

3.    Declaring variables and assigning values

 

So far we have worked with objects (also called 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.



 
 

 

 

 

 

 

 

 

 

 

 


  1. Go into your code box.  Select General from the object drop-down menu.  Declarations should automatically come up in the procedure window.

  2. Add the following two words to the General area (the very top of your code page):

    Option Explicit

Text Box: **NOTE** 
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.

 

  1. Use the name studentInfo as a variable and declare it.  We want this variable to hold text, so we’ll declare it as a String variable.  Enter the following statement after the Option Explicit statement:

    Dim studentInfo as String

 


 

  1.  Now, here’s the tricky part.  We want to use this variable to hold some information about you, the creator of this program.  We want this info to appear when a user clicks on the lblALabel object.  

·         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() event procedure, initialize your variable studentInfo by adding the statement:

 

studentInfo = “Hello there!  This is <your name>, and I wrote the code for this program!”

Text Box: **NOTE** 
You only want the value of studentinfo 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.

 

 

 

 

 

 

  1. 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 = studentInfo

 

 

  1. Run your program. 

·         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 studentInfo appears as the caption of the label.

·         Trés cool, no?

  1. Save your project again.

 

4.    Creating an Executable File

  1. Make your project executable when you are finished and happy with it.  Look in the File Menu for the correct command.  It really is just as simple as pressing a button!

  2. You work has been saved to the Lab8 folder during class time.  Rename the folder Lab9_yourname.  Now, upload the whole folder back to your Dante account.  You should have a Lab 8 and Lab 9 folder loaded to the server.

 

 

5.    Looking ahead to Lab 10

 

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> Then

                   <perform some action>

          Else

                   <perform a different action>

          End If

 

The syntax (the way this statement is written out-grammar) 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 give their birth date.  Once they enter that information and click on a submit button, the program tells them their sign.

 

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

12 option buttons (radio buttons)

 

To get a head start on Lab 10, create a project that has a form with the following objects.  Save it in a folder called Lab10_yourname and upload the entire folder to Dante for use in lab next week.  Your form does not have to look like this one-but it should include all the objects mentioned.

**A good way to keep track of the names would be to print out the figure
above and put the name you will give each control next to its image**

 

 

6.    Lab Notebook Questions

 

  1. What happens when you click on the Yellow or Green buttons?  Explain in detail how your program knows what to do.  Use the idea of events in your answer.  The answer “Turns background yellow or green” is not enough of an answer.


  2. Variable assignment is the process of substituting values.  Explain what this means.