If...Else Activity 3.1.1 to 3.1.4

advertisement
If...Else Activity 3.1.1 to 3.1.4
Topics Covered:
Creating Frames & Option Buttons
Using Logical Operators
Using the SetFocus function
Creating Check Boxes
"Not So Basic" - Gas Bar Program
In order for you to learn the fundamentals of selection the program titled "NOT SO BASIC" GAS BAR will provide you with a firm ground of the if..then..else statement, check boxes,
option boxes, frames, logical and relational operators.
NOTE: If you have problems with the content of this program, go back to the IF...ELSE
THEORY PAGE
Program Background
The "NOT SO BASIC" - GAS BAR allows the user to choose from gas tank fill ups to car
maintenance. The user will be able to click on the type of gas their car requires as well as if the
car owner would like full service or self service. Other forms such as car wash options and bill
total could be used in this program will be developed by you the programmer. Your completed
program could be used as a gas companies computerized tracking system.
1. Create the form Above.
Activity 3.1.1- Gas Grade and Service Type
Creating the Frames and Option Button Controls
Option buttons allow users to interact with a program. The user can choose an option rather than
typing in text. Option buttons display an option that can be turned on or off.
OptionButton controls used in a form display options from which the user selects only one. In
the "Not so Basic"- Gas Bar program you will group OptionButton controls by drawing them
inside a container such as a Frame control.
Before you decide if you would like many options on one form, you need to consider if you
would like to click on more than one option box at the same time. Only one option can be chosen
at the same time.
In the "Not so Basic" - Gas Bar" the gas types will be grouped together and the types of services
can be grouped together. Not only can a frame be used to group the options, a PictureBox object,
or a form can also be used. Make sure when creating the option groups in a Frame or PictureBox,
always draw the Frame or PictureBox first, and then draw the OptionButton objects inside. All
OptionButton objects/controls within the same container act as a single group. For the Gas Bar
program we will be using the Frame object to group the gas types and services.
While OptionButton controls and CheckBox controls may appear to function similarly, there is
an important difference: When a user selects an OptionButton, the other OptionButton controls
in the same group are automatically unavailable. In contrast, any number of CheckBox controls
can be selected.
The values of an option box maybe False for off and True for on. The default value for the option
box is always False.
Example This option box has the Value True.
Example
optBronze.Value = true
Adding in the Type of Service
Create the frame fraServiceType first and add in the two option boxes into the frame.
Object
Name Property
Text
Property
Caption Property
Frame
fraServiceType
TYPE OF SERVICE
Option Box 1
optSelfService
SELF SERVICE
Option Box 2
optFullService
FULL SERVICE
Adding in the Gas Grade
Don't forget to add in the frame before adding in the option boxes.
Object
Name Property
Frame
fraGasGrade
Text
Property
Caption Property Variable
Name
GAS GRADE
Option Box 1
optBronze
BRONZE
Option Box 2
optSilver
SILVER
Option Box 3
optGold
GOLD
Label 1
lblBronze
59.99
Label 2
lblSilver
65.00
Label 3
lblGold
69.99
Command
Box - Gas
Total
cmdGasTotal
GAS TOTAL
Text Box for
Gas Total
txtGasTotal
Label for
Liters of Gas
lblNumLiters
Text Box for
Liters
Purchased
txtNumLiters
Clear
N/A
GasTotal
ENTER THE
NUMBER OF
LITERS
PURCHASED
Clear
N/A
Command for cmdClear
Clear
CLEAR ORDER
Command for cmdExit
Exit
E&XIT
NumLiters
Activity 3.1.2
Variable Declaration For The Program
Declaring the variables needed for the project will include the currency, integer data types as
well as constants. Don't forget to add the variables in the form so that they can be used
throughout the form.
ASIDE: For those that would like to create the car wash and final bill total forms you will need
to declare all variables globally in a module.
Option Explicit
Dim GasPrice As Currency, GasTotal As Currency
Dim NumLiters As Integer
Const FullService = 1.55
Const BronzePrice = 0.5999
Const SilverPrice = 0.65
Const GoldPrice = 0.6999
Currency is used for the gas price and gas total so that the totals will appear with the appropriate
two decimal places. After all the government wants our change.
Constants are used so that is there is a change in the gas price from day to day, the programmer
can go into the variable declaration section of the program and make changes. This saves the
programmer time since he/she does not have search through the code for the gas prices.
Activity 3.1.3
Logical Operators
It is possible in selection (both types) to conduct more that one test function to find if a
condition is true. This could be done with the nested if statement discussed above or with
Logical Operators.
You can use logical operators to combine several conditions into one compound condition.
Logical Operators are also termed compound operators because they compare the results
of two or more test conditions.
Operator
Description
And
Conjunction
All conditions are connected by the And operator must be true for the
conditions to be true, either compound or more than two conditions.
Example:
If optBronze = True And optSelfServe = True Then
Or
Disjunction
Only one of the conditions connected by the Or operator needs to be true.
Example:
if chkOilCheck = 0 Or chkWasherFluid = 0 Or chkWindowClean = 0 then
Xor
Exclusive Or
This returns a true value only if one of the variables is True.
Not
Complement
This flip flops the true and false conditions. True becomes false and false
becomes true. Reverses the value of the condition.
Eqv
Equivalence
Imp
Implication
Example With And
The example displays a message that depends on the value of variables A, B, and C, assuming
that no variable is a Null.
If A = 10, B = 8, and C = 6, both expressions evaluate True.
Because both expressions are True, the And expression is also True.
Private Sub Form_Click ()
Dim A as Integer, B as Integer, C as Integer ' Declare variables.
Dim Msg
A = 10: B = 8: C = 6 ' Assign values.
If A > B And B > C Then ' Evaluate expressions.
Msg = "Both expressions are True."
Else
Msg = "Both expressions are False."
End If
MsgBox Msg ' Display results.
End Sub
Code for the Gas Total (cmdGasTotal)
SetFocus allows the cursor to appear on the desired object. TabIndex would also allow the
cursor to begin on a certain object on a form.
To use SetFocus see the code below:
Private Sub cmdGasTotal_Click()
' Assign the text box to a variable
txtNumLiters.SetFocus '<------HINT: SetFocus will place the cursor in the first text box when
the program is executed.
NumLiters = Val(txtNumLiters.Text)
' Making sure that the user does not forget to enter in the liters purchased.
If NumLiters = 0 Then ***HINT: Make Sure all If constructs are properly indented to
improve readability***
MsgBox ("PLEASE enter the NUMBER OF LITERS you have purchased!")
Else
' the price of gas is calculated based on the type of gas purchased
' and the number of liters purchased
If optBronze.Value = True And optSelfServe.Value = True Then
GasTotal = BronzePrice * NumLiters
ElseIf optSilver.Value = True And optSelfServe.Value = True Then
GasTotal = SilverPrice * NumLiters
ElseIf optGold.Value = True And optSelfServe.Value = True Then
GasTotal = GoldPrice * NumLiters
ElseIf optFullService.Value = True And optBronze.Value = True Then
' if full service is chosen the gas price increases by a 1.50 service charge.
GasTotal = (BronzePrice * NumLiters) + FullService
ElseIf optFullService.Value = True And optSilver.Value = True Then
GasTotal = (SilverPrice * NumLiters) + FullService
ElseIf optFullService.Value = True And optGold.Value = True Then
GasTotal = (GoldPrice * NumLiters) + FullService
Else
MsgBox ("Choose the GAS and SERVICE type")
End If
End If
txtGasTotal.Text = Format$(GasTotal, "Currency")
End Sub
Activity 3.1.4
Creating the Check Boxes
Description
Check box, option button-determines the state of the control.
A check box displays an when selected; the disappears when the check box is cleared (in
VB 4.0. In VB 3.0 the check box displays an X.
Use this control to give the user a 0-Unchecked or a 1-Checked option.
You can use check boxes in groups to display multiple choices from which the user can
select one or more. This is the benefit of check boxes versus option boxes. In our
example of the gas program, the different maintenance choices can be chosen all at
once, one or two of the choices. This makes sense because most gasoline bars offer
these maintenance choices if you have full service.
The text styles include:
bold
underlined
strikethru
FontSizes- 14
Check boxes and option buttons function similarly but with an important difference:
any number of check boxes on a form can be selected at the same time. In contrast,
only one option button in a group can be selected.
To display text next to the check box, set the Caption property. Use the Value property
to determine the state of the box selected, cleared, or unavailable.
EXAMPLE OF CODE:
chkOilCheck.Caption = True
Object
Name Property
Text
Property
Caption Property
Frame
fraMaintenance
CAR MAINTENANCE
Check Box 1
chkOilCheck
Oil Check
Check Box 2
chkWasherFluid
Wind Shield Washer
Fluid
Check Box 3
chkWindowClean
Windows Cleaning
Command for
Maintenance Check
cmdMaintenance
CLICK HERE FOR
MAINTENANCE
CHECKS
Creating the Code the Maintenance Check (cmdMaintenance)
Using the If..ElseIf code statements for the command button is as follows:
Private Sub cmdMaintenance_Click()
' Only if the car drives into the full service bay will the maintenance checks
' be provided.
If optFullService = True Then ***HINT: Make Sure all If constructs are properly indented
to improve readability***
If chkOilCheck = 1 And chkWasherFluid = 0 And chkWindowClean = 0 Then
MsgBox ("OIL CHECKED")
ElseIf chkOilCheck = 0 And chkWasherFluid = 1 And chkWindowClean = 0 Then
MsgBox ("WASHER FLUID CHECKED")
ElseIf chkOilCheck = 0 And chkWasherFluid = 0 And chkWindowClean = 1 Then
MsgBox ("WINDOWS CLEANED")
ElseIf chkOilCheck = 1 And chkWasherFluid = 1 And chkWindowClean = 0 Then
MsgBox ("OIL AND WASHER FLUID CHECKED")
ElseIf chkOilCheck = 0 And chkWasherFluid = 1 And chkWindowClean = 1 Then
MsgBox ("WASHER FLUID AND WINDOWS CLEANED")
ElseIf chkOilCheck = 1 And chkWasherFluid = 0 And chkWindowClean = 1 Then
MsgBox ("OIL CHECKED AND WINDOWS CLEANED")
ElseIf chkOilCheck = 1 And chkWasherFluid = 1 And chkWindowClean = 1 Then
MsgBox ("FULL MAINTENANCE CHECK PERFORMED")
Else
MsgBox ("No Maintenance check performed")
End If
Else
MsgBox ("You must go to the full service area of the gas bar to receive car maintenance!")
End If
End Sub
Run the programm
Extension:
The command button "CLICK HERE FOR CAR WASH" should lead the user into another form.
For example:
You can also add a
TOTAL GAS BAR BILL
form. This is left up to
your imagination.
Download