FIT 100

Lab 11:  Conditionals and Procedures

Spring 2001

Reading to be done prior to Lab 11:

·         p. 127-141 (read through Programming Tips) in Chapter 6 of Computer Programming Fundamentals with Applications in Visual Basic 6.0

 

 

Introduction:

You have been introduced to the idea of conditionals as a way to write code that allows a program to “make choices” based on user input.  This lab and the next combine condition statements with procedure calls as you create a voter’s ballot that tallies the votes for two candidates.

 

Objectives:

  1. To obtain more practice with the VB IDE.
  2. To use condition statements to direct the flow of your program.
  3. To write statements that pass parameters in a procedure call.

 

TO DO: 

  1. Open up a new project in Visual Basic. 

 

  1. Rename your project ChicagoVote. 

 

  1. Create a form with the following controls on it:

 

·         5 Labels

·         2 Command Buttons

·         2 Radio Buttons

Name your form frmVote.  Make the caption “Lab 11”.

  1. Spend a few minutes changing the names and captions of the controls you put on your form.  Change the colors and fonts to something you like as well.  This lab, you won’t be given the names for your objects. You will need to decide on a naming scheme and be able to use the names of your objects appropriately in your code. 

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

 

Your form will look something like this when you are done (use the colors and fonts of your choice):

Make this button invisible at first

 

Labels

 

Command Buttons

 

Radio Buttons

 


  1. Save your form and your project.

 

The Scenario:

This is the ballot for the presidential elections for the Chicago area in 2004.  Your goal is to write a program that will increment the number of votes for your first candidate (George) by 1 when his radio button is selected and the Voter clicks the Vote button.  But when someone chooses candidate Jordan, the sly and underhanded pollsters of the town have set it up so that the vote tally is incremented by 5 instead.  After ten clicks of the button, the Vote button will disappear and the “Take a break button” will appear.  (This is an attempt at fairness so that others will be able to vote!)

 

You will need a number of variables declared to handle (1) the vote count for each candidate, (2-3) to keep track of the candidates themselves determining who should get the vote, and (4) to keep track of the number of times the Vote button has been clicked.  Decide on a name for the following variables and declare them:

  1. The first step is to declare variables at the top of the code box so that they are available to all sub-routines of the program.

    Dim <some first variable> as String
    Dim <some second variable> as Integer, <some third variable> as Integer, <some fourth variable> as Integer

(Note that you can declare all your variables on one line if you wish, as long as you indicate the data type for each)

           

  1. When the form loads, we will control the values that are assigned to each of our variables.  In the form load event, assign 0 to each of your integer variables.  This starts the tally amounts over for each candidate and starts the click count at zero as well.

  2. Now go to the click event for each of your radio buttons. 

·         In the click event for the radio button with the caption George Bush, assign the string value “George” to your candidate variable. 

·         In the click event for the radio button with the caption Michael Jordan, assign the string value “The Great One” to your candidate variable.

Writing these statement will change out the value of the variable.  This will help when you write the code to tally the votes for each.

You have taken care of initializing variables and establishing the values for the candidate variable in each of the radio buttons.  Now, where will the next piece of action take place?  The click event procedure for the Vote button.  Once a person selects a candidate by clicking a radio button, they will then click the Vote button to place their vote. 

  1.  Several things need to happen in the click event of the Vote button.

·         The click count must increase by one (this will happen each time the button is clicked).

·         A check must be done on the value in the candidate variable.  If the value is equal to “George” then we will increase the tally for George by one and display it in a label.  But if the value isn’t “George”, and instead it is equal to “The Great One”, then we want to increase the tally for Michael Jordan by 5 and display it in a separate label. 

·         If the value in the candidate variable is neither of these, then the user has not selected a radio button and we want to alert them to do so with a message box.  Use the following structure for your message box:

MsgBox (“Please select a candidate!”), vbOKOnly 

          Write the If-Elseif-Else statements that will do the above operations inside of the Vote button click event procedure.

  1. Run your program.  Do the labels display the correct number of votes for the candidates?
     
  2. Save your program.

  3. Write a procedure called ClickCheck.  The parameter of this procedure is the variable numberOfClicks as Integer.  In this procedure you will check to see if the numberOfClicks is greater than 9.  If it is, then hide the vote button and make the other command button with the caption “Ok, take a break- Next?” appear.

 

Private Sub ClickCheck (numberOfClicks as Integer)

          If numberOfClicks  > 9 then

                   <statement to make Vote Button invisible>

                   <statement to make Next Button visible>

          End If

End Sub

  1. Where is a logical place to call this procedure?  What would the argument of this procedure be?

    The logical place to call it would be just after we increase the click count variable by one in the click event procedure for the Vote button. 
    The argument to pass would be the value in click count.

 

Call ClickCheck (<name of click count variable>)

 

          It appears as if this procedure call is not necessary since it is only used once, but we will add arguments and parameters to it for

          Lab 12.


  1. The last statements to add for this lab are in the click event procedure for the other command button.

·         On click, make the Vote button visible and hide the Next button

·         Assign 0 to the clickcount variable

 

  1. Run your program. 

·         On each click of the vote button does the number of votes for the candidate increase the proper amount and display in the correct label?

·         After 10 clicks, does the Vote button disappear and the other button appear?

·         When you click the Next button, does the Vote button reappear and the Next button disappear.

·         Can you continue voting?

 

  1. Save your project to disk or FTP it to your Dante account.  You will need it for Lab 12

 

Are we having fun yet???!?!?!?!?!?!? J