Practical Learning: Introducing Check Boxes
|
|
- Start a new Windows Application named DaniloPizza1
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Exercise.vb and press Enter
- On the main menu, click Project -> Add New Item...
- In the Templates list, click Icon File
- Set the Name to pizza and click Add
- Using the Erase tool
,
wipe its interior to erase its content
- Use the Ellipse tool
and the
black color to draw an oval shape
- Fill
it up with a brown color (the color on the right side of the black)
- Click the red color
- Click the Airbrush tool
- Click the option button and select the Medium brush
- Randomly click different parts of the shape
- To give the appearance of a crust, use the Pencil tool
, click the
black color, right-click the brown color, and draw a dark border around the
shape
- With still the black and the brown colors, randomly click and
right-click different points in the shape:
- Click the white color. Right-click the yellow color. Randomly click and
right-click a few times in the shape
- Click the brown color again and click the Line tool
- Randomly draw horizontal, vertical, and dialog lines in the shape
- Right-click a white section in the drawing area, position the mouse on
Current Icon Image Types, and click 16x16, 16 Colors
- Design it as follows:
- Save the icon and close it
- Use the Icon field of the form in the Properties window to assign the
pizza.ico icon to the form
- Design the form as follows:
|
Control |
Text |
Name |
Additional Properties |
GroupBox |
|
Pizza Size |
|
|
RadioButton |
|
Small |
rdoSmall |
|
TextBox |
|
8.95 |
txtSmall |
AlignText: Right |
RadioButton |
|
Medium |
rdoMedium |
Checked: True |
TextBox |
|
10.75 |
txtMedium |
AlignText: Right |
RadioButton |
|
Large |
rdoLarge |
|
TextBox |
|
12.95 |
txtLarge |
AlignText: Right |
GroupBox |
|
Side Orders |
|
|
Label |
|
Qty |
|
|
Label |
|
Unit Price |
|
|
Label |
|
Sub Total |
|
|
Label |
|
Bread Sticks |
|
|
TextBox |
|
0 |
txtQtyBread |
AlignText: Right |
TextBox |
|
3.25 |
txtPriceBread |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalBread |
AlignText: Right
ReadOnly: True |
Label |
|
Buffalo Wings |
|
|
TextBox |
|
0 |
txtQtyWings |
AlignText: Right |
TextBox |
|
2.15 |
txtPriceWings |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalWings |
AlignText: Right
ReadOnly: True |
GroupBox |
|
Toppings |
|
|
CheckBox |
|
Pepperoni |
chkPepperoni |
|
CheckBox |
|
Sausage |
chkSausage |
|
CheckBox |
|
Extra Cheese |
chkExtraCheese |
|
CheckBox |
|
Olives |
chkOlives |
|
CheckBox |
|
Onions |
chkOnions |
|
Label |
|
Each Topping |
|
|
TextBox |
|
0.45 |
txtEachTopping |
AlignText: Right |
GroupBox |
|
Drinks |
|
|
Label |
|
Qty |
|
|
Label |
|
Unit Price |
|
|
Label |
|
Sub Total |
|
|
Label |
|
Soda Can |
|
|
TextBox |
|
0 |
txtQtyCan |
AlignText: Right |
TextBox |
|
1.45 |
txtPriceCan |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalCan |
AlignText: Right
ReadOnly: True |
Label |
|
Soda 20 Oz. |
|
|
TextBox |
|
0 |
txtQtySoda20 |
AlignText: Right |
TextBox |
|
1.45 |
txtPriceSoda20 |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalSoda20 |
AlignText: Right
ReadOnly: True |
Label |
|
Soda 2L Bottle |
|
|
TextBox |
|
0 |
txtQtySoda2L |
AlignText: Right |
TextBox |
|
1.45 |
txtPriceSoda2L |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalSoda2L |
AlignText: Right
ReadOnly: True |
Label |
|
Orange Juice |
|
|
TextBox |
|
0 |
txtQtyOJ |
AlignText: Right |
TextBox |
|
2.25 |
txtPriceOJ |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalOJ |
AlignText: Right
ReadOnly: True |
Label |
|
Water |
|
|
TextBox |
|
0 |
txtQtyWater |
AlignText: Right |
TextBox |
|
1.25 |
txtPriceWater |
AlignText: Right |
TextBox |
|
0.00 |
txtTotalWater |
AlignText: Right
ReadOnly: True |
Button |
|
Close |
btnClose |
|
Label |
|
Total Price |
|
|
TextBox |
|
0.00 |
txtTotalPrice |
AlignRight: Right
ReadOnly: True |
|
- Save everything
- Right-click the form and click View Code
- Inside the class, create the following procedure:
Public Class Exercise
Private Sub CalculatePrice()
Dim PriceSize As Double
Dim PriceEachTopping As Double
Dim BWings As Double
Dim Bread As Double
Dim SodaCan As Double
Dim Soda20 As Double
Dim Soda2L, OJ As Double
Dim Water As Double
Dim PriceToppings As Double
Dim TotalOrder As Double
Dim Pepperoni As Integer
Dim Sausage As Integer
Dim ExtraCheese As Integer
Dim Onions As Integer
Dim Olives As Integer
Try
' Get the price of pizza depending on the selected size
If rdoSmall.Checked = True Then
PriceSize = CDbl(txtSmall.Text)
End If
If rdoMedium.Checked = True Then
PriceSize = CDbl(txtMedium.Text)
End If
If rdoLarge.Checked = True Then
PriceSize = CDbl(txtLarge.Text)
End If
Catch ex As Exception
MsgBox("The value you typed for the " &
"price of a pizza is invalid" &
vbCrLf & "Please try again")
End Try
' Get the price of a topping if it was selected
If chkPepperoni.Checked = True Then
Pepperoni = 1
Else
Pepperoni = 0
End If
If chkSausage.Checked = True Then
Sausage = 1
Else
Sausage = 0
End If
If chkExtraCheese.Checked = True Then
ExtraCheese = 1
Else
ExtraCheese = 0
End If
If chkOnions.Checked = True Then
Onions = 1
Else
Onions = 0
End If
If chkOlives.Checked = True Then
Olives = 1
Else
Olives = 0
End If
' Get the price of each topping
Try
PriceEachTopping = CDbl(txtEachTopping.Text)
Catch
MsgBox("The value you typed for the price " &
"of a each topping is invalid" &
vbCrLf & "Please try again")
End Try
PriceToppings = (Pepperoni + Sausage + ExtraCheese +
Onions + Olives) * PriceEachTopping
' Calculate the price of the side dishes
' depending on the quantity entered
BWings = CDbl(txtTotalWings.Text)
Bread = CDbl(txtTotalBread.Text)
' Calculate the price of the drink(s)
SodaCan = CDbl(txtTotalCan.Text)
Soda20 = CDbl(txtTotalSoda20.Text)
Soda2L = CDbl(txtTotalSoda2L.Text)
OJ = CDbl(txtTotalOJ.Text)
Water = CDbl(txtTotalWater.Text)
TotalOrder = PriceSize + PriceToppings + BWings + Bread +
SodaCan + Soda20 + Soda2L + OJ + Water
txtTotalOrder.Text = FormatNumber(TotalOrder)
End Sub
End Class
|
- Under the above procedure, create the following procedure:
Private Sub rdoSmallClick(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles rdoSmall.Click,
rdoMedium.Click,
rdoMedium.Click,
chkExtraCheese.Click,
chkOlives.Click,
chkOnions.Click,
chkPepperoni.Click,
chkSausage.Click
CalculatePrice()
End Sub
|
- Under the above procedure, create the following procedure:
Private Sub ItemPriceLeave(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles txtSmall.Leave,
txtMedium.Leave,
txtLarge.Leave,
txtMedium.Leave,
txtLarge.Leave,
txtEachTopping.Leave,
txtQtyBread.Leave,
txtPriceBread.Leave,
txtQtyWings.Leave,
txtPriceWings.Leave,
txtQtyCan.Leave,
txtPriceCan.Leave,
txtQtySoda20.Leave,
txtPriceSoda20.Leave,
txtQtySoda2L.Leave,
txtPriceSoda2L.Leave,
txtQtyOJ.Leave,
txtPriceOJ.Leave,
txtQtyWater.Leave,
txtPriceWater.Leave
Dim QtyBread As Integer
Dim QtyWings As Integer
Dim QtyCan As Integer
Dim QtySoda20 As Integer
Dim QtySoda2L As Integer
Dim QtyOJ As Integer
Dim QtyWater As Integer
Dim PriceBread As Double
Dim TotalBread As Double
Dim PriceWings As Double
Dim TotalWings As Double
Dim PriceCan As Double
Dim TotalCan As Double
Dim PriceSoda20 As Double
Dim TotalSoda20 As Double
Dim PriceSoda2L As Double
Dim TotalSoda2L As Double
Dim PriceOJ As Double
Dim TotalOJ As Double
Dim PriceWater As Double
Dim TotalWater As Double
' Retrieve the quantity set in the text box
Try
QtyBread = CInt(txtQtyBread.Text)
Catch ex As Exception
MsgBox("The value you entered for the quantify " &
"of bread sticks is not valid" &
vbCrLf & "Please try again!")
End Try
' Get the unit price of the item
Try
PriceBread = CDbl(txtPriceBread.Text)
Catch ex As Exception
MsgBox("The value you entered for the price " &
"of bread sticks is not valid" &
vbCrLf & "Please try again!")
End Try
' Calculate the sub-total of this item
TotalBread = QtyBread * PriceBread
' Display the sub-total in the corresponding text box
txtTotalBread.Text = TotalBread.ToString("F")
Try
PriceWings = CInt(txtQtyWings.Text)
Catch ex As Exception
MsgBox("The value you entered for the quantify " &
"of orders of buffalo wings is not valid" &
vbCrLf & "Please try again!")
End Try
Try
PriceWings = CDbl(txtPriceWings.Text)
Catch ex As Exception
MsgBox("The value you entered for the price " &
"of buffalo wings is not valid" &
vbCrLf & "Please try again!")
End Try
TotalWings = QtyWings * PriceWings
txtTotalWings.Text = TotalWings.ToString("F")
Try
QtyCan = CInt(txtQtyCan.Text)
Catch ex As Exception
MsgBox("The value you entered for the " &
"quantify of soda cans is not valid" &
vbCrLf & "Please try again!")
End Try
Try
PriceCan = CDbl(txtPriceCan.Text)
Catch ex As Exception
MsgBox("The value you entered for the " &
"price of soda cans is not valid" &
vbCrLf & "Please try again!")
End Try
TotalCan = QtyCan * PriceCan
txtTotalCan.Text = TotalCan.ToString("F")
Try
QtySoda20 = CInt(txtQtySoda20.Text)
Catch ex As Exception
MsgBox("The value you entered for the " &
"quantify of soda 20 Oz. is not valid" &
vbCrLf & "Please try again!")
End Try
Try
PriceSoda20 = CDbl(txtPriceSoda20.Text)
Catch ex As Exception
MsgBox("The value you entered for the " &
"price of soda 20 Oz. is not valid" &
vbCrLf & "Please try again!")
End Try
TotalSoda20 = QtySoda20 * PriceSoda20
txtTotalSoda20.Text = TotalSoda20.ToString("F")
Try
QtySoda2L = CInt(txtQtySoda2L.Text)
Catch ex As Exception
MsgBox("The value you entered for the quantify " &
"of bottles of soda 2-litter is not valid" &
vbCrLf & "Please try again!")
End Try
Try
PriceSoda2L = CDbl(txtPriceSoda2L.Text)
Catch ex As Exception
MsgBox("The value you entered for the price " &
"of a bottle of soda is not valid" &
vbCrLf & "Please try again!")
End Try
TotalSoda2L = QtySoda2L * PriceSoda2L
txtTotalSoda2L.Text = TotalSoda2L.ToString("F")
Try
QtyOJ = CInt(txtQtyOJ.Text)
Catch ex As Exception
MsgBox("The value you entered for the quantify " &
"of a bottle of orange juice is not valid" &
vbCrLf & "Please try again!")
End Try
Try
PriceOJ = CDbl(txtPriceOJ.Text)
Catch ex As Exception
MsgBox("The value you entered for the price " &
"of bottles of orange is not valid" &
vbCrLf & "Please try again!")
End Try
TotalOJ = QtyOJ * PriceOJ
txtTotalOJ.Text = TotalOJ.ToString("F")
Try
QtyWater = CInt(txtQtyWater.Text)
Catch ex As Exception
MsgBox("The value you entered for the quantify " &
"of bottles of water is not valid" &
vbCrLf & "Please try again!")
End Try
Try
PriceWater = CDbl(txtPriceWater.Text)
Catch ex As Exception
MsgBox("The value you entered for the price " &
"of bottle of water is not valid" &
vbCrLf & "Please try again!")
End Try
TotalWater = QtyWater * PriceWater
txtTotalWater.Text = TotalWater.ToString("F")
CalculatePrice()
End Sub
|
- In the Class Name combo box, select btnClose
- In the Method Name combo box, select Click and implement the event as
follows:
Private Sub btnCloseClick(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles btnClose.Click
End
End Sub
|
- Execute the application and test the form. Here is an example:
- Close the form and return to your programming environment
Download
|
|