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:
TO DO:
· 5 Labels
· 2 Command Buttons
·
2 Radio Buttons
Name your form frmVote. Make the caption “Lab 11”.
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
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:
(Note that you can declare all
your variables on one line if you wish, as long as you indicate the data type
for each)
· 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.
· 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.
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
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.
· On click, make the Vote button visible and hide the Next button
· Assign 0 to the clickcount variable
· 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?
Are
we having fun yet???!?!?!?!?!?!? J