Lab 10: Creating the “What’s Your Sign, Dude?” Application
Spring 2002
1. Creating the Interface for SignFinder:
2. Creating Variables to hold values
3. Assigning Values to Variables
6. Creating an Executable File:
Reading for Lab 10:
· 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 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.
If you have created a folder holding
the form and all required controls for Lab 10 already, download it from Dante
to the desktop. Then go to that project and open it up and move to step 2.
If you have not created the form yet, open a new Standard EXE and in the form,
enter the following controls:
Your form (the interface) for this lab will require the following objects:
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:
**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**
|
|
A. Declare 4 separate variables at the top of your program after you enter
the Option Explicit statement:
·
The first variable will hold the Zodiac sign value
for the lower half of each month and be a string data type: loSign
·
The second variable will hold the Zodiac sign value
for the upper half of each month and be a string data type: hiSign
·
The third variable will hold the value of the day
each month when the signs change and be an integer data type: changeDay
· The fourth variable will hold the value entered by the user that is their birth date and be an integer data type: birthday
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
o
Assign the value the user enters into the text box
to the variable birthDay.
To get to the value entered in the text box, access the Text property.
o Make the label which will display the person’s sign and the Go Again command button appear and the text box and OK button disappear. Set the visibility properties to true or false where needed.
o
Write a general conditional that will check to see
if the value the user has entered is greater than the changeDay.
If it is, then the label that you are using to display the Zodiac sign should
display “Your sign is ” & hiSign & “!” in the caption.
If it isn’t greater than changeDay, then the caption will read: “Your
sign is ” & loSign & “!”
(Think about resetting the option buttons and clearing out the text box.)
|
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
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
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 I’ll let you add to the code to perfect it if you like!