FIT 100
Lab 9: Creating
the “What’s Your Sign, Dude?” Application
Winter 2002
Recommended Reading for
Lab 9:
·
p. 97-105 in Chapter 5
of Computer Programming Fundamentals with Applications in Visual Basic 6.0
·
p. 151 -154 in Chapter 7
of Computer Programming Fundamentals with Applications in Visual Basic 6.0
Today you will create a program and call it SignFinder. In this program, a user will click on the radio button for the month of their birth but they will enter the exact date of their birth as well. When the OK button is clicked, the program will have to decide which sign to display depending on where their birth date falls in the month.
· 2 labels
· 2 command buttons
· 1 text box
·
12 option buttons (radio buttons)
The interface may look similar to the
one below, but use whatever colors and setup that works for you:
You will need to give meaningful names to these objects so that as you code, you will be able to identify them properly. For example, a good name for the label object that invites the user to enter some information would be lblEnter. A good name for the January option button would be optJan.
We suggest that you use the following VB naming convention. The name starts with a prefix that indicates the type of the object, followed by some short string corresponding to the meaning of the object. For example, in optJan, “opt” indicates that this is an option button; “Jan” indicates that this is the button for January. The prefixes are listed in the following table:
Object Type Name starts with
Form frm
Command Button cmd
Text Box txt
Label lbl
Option Button opt
Picture Box pic
CheckBox chk
Timer tmr
**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.**
Logic of the program:
Once you have the form (interface) taken care of, it’s time to deal with the program logic – and then create the corresponding code. Here is how your program should behave.
When the user starts your program, the form pops up and they are requested to select the option button that states the month of their birthday and to enter an integer value that is their birth date. Once they have done this, they click on the OK command button and a label appears on the form that states their Zodiac sign The Go Again button appears as well. At this point the text box and OK button disappear (they have done their job). Clicking on the Go Again button makes them reappear and ready for more user input.
An important detail to notice is that the signs of the Zodiac do not have a one-to-one correspondence with each month. If the birth date falls on the earlier part of the month, the person is of one sign, which we will call “low sign.” Otherwise, the person is of another sign, which we will call “high sign.” The day of the month up to (and including) which the person is of the low sign will be called the “change day.” Finally, low sign, high sign and change day all depend on the month of the birthday.
You will need to create a place to store the two Zodiac values and the change day for each month, and also to store the input from the user. Finally, you will need to use a conditional to decide whether the person’s sign is the low sign or the high sign.
Creating Variables to hold values:
LoSign and hiSign - to
hold the low and high signs for the selected month; they should be of string
data type;
ChangeDay - to hold the change
day for the selected month; it should be of integer data type;
birthday - to hold the value
entered by the user that is their birth date; it should also be of integer data
type.
Assigning Values to Variables:
The program will store the low
and high signs and the change day into the above variables when the user clicks
on an option button for some month.
Correspondingly, the code to do so needs to appear as a handler for the
click event.
Month loSign hiSign
changeDay
January “Capricorn” “Aquarius” 19
February “Aquarius” “Pisces” 18
March “Pisces” “Aries” 20
April “Aries” “Taurus” 19
May “Taurus” “Gemini” 20
June “Gemini” “Cancer” 20
July “Cancer” “Leo” 22
August “Leo” “Virgo” 22
September “Virgo” “Libra” 22
October “Libra” “Scorpio” 22
November “Scorpio” “Sagittarius” 21
December “Sagittarius” “Capricorn” 21
Now it’s time to work with the OK
button click event. Most of the program
logic occurs here.
Click
Event Procedures
The & symbol
is used to concatenate string values together [place them together, side by
side]. If a value in a variable is an
integer, it is converted to a string value for purposes of concatenation.
Simple Error Trapping
Often the user will not enter the
information we (as programmers) wish them to enter. If they do, for example, enter letters when we expect numbers and
we haven’t planned for it, it will stop our program. We say that an error has occurred. The program may want to react to such error by Error Trapping. For example, the program may let the user
know they have entered incorrect information and ask them to enter it again.
The syntax used to start an Error Trap statement is the following:
Private Sub ….. (this could be any event procedure)
On Error GoTo <name of error handler>
<other code statements in the procedure>
Exit Sub
<name of error handler>
<statements to be executed when error occurs>
End Sub
The best place to trap any errors
in this program is in the OK button click event, since all the other logic is
taking place there as well. Call the
error handler CheckInput, since it is checking input. You will use a Message box in the error
handler statements as a way to alert the user to change their input.
Remember, if you haven’t used the same variable names and object names IT’S OK! Your code will look different from the statements below (except the statements that are in bold – those should look same):
Private Sub cmdOK_Click()
On Error GoTo CheckInput
birthDay = txtUserInput.Text
If birthDay > changeDay Then
lblZodiac.Visible = True
lblZodiac.Caption = "Your sign is " & hiSign & "!"
Else
lblZodiac.Visible = True
lblZodiac.Caption = "Your sign is " & loSign & "!"
A Message Box is used in VB 6
to communicate with the user. It
can alert them to enter valid data, confirm an action or simply keep the
user informed. Here the box will
use an “OK” button as a way to let the user confirm the message.
End If
cmdOK.Visible = False
cmdGoAgain.Visible = True
txtUserInput.Visible = False
Exit Sub
CheckInput:
MsgBox
"Please enter a valid date and check a month" , VBOKOnly
txtUserInput.SetFocus
End Sub
What happens if you enter a
number that is negative or above the days in the month? The error trap doesn’t yet have a way to
account for that, so we need another conditional to take care of those
potential errors.
birthDay = txtUserInput.Text
If birthDay < 1
Or birthDay > 31 Then GoTo CheckInput
If birthDay > changeDay Then
lblZodiac.Visible = True
lblZodiac.Caption = "Your sign is " & hiSign & "!"
Else
lblZodiac.Visible = True
lblZodiac.Caption = "Your sign is " & loSign & "!"
End If
The bold code statement above now checks to see that the number a user enters is inside of a certain range (between 1 and 31). If it isn’t, the message box comes up again and tells them to check their input. This error trap isn’t perfect (what about months with less than 31 days?), but we will handle that later.
Creating an Executable File: