Sie sind auf Seite 1von 12

Danilo Pizza

This application is used to explore the fundamental characteristics of radio buttons and
check boxes. A clerk uses radio buttons to select the size of a pizza (small, medium, or
large). Some check boxes are used to select the toppings. A function is used to calculate the Page | 1
cost of the pizza. This is done live as the user makes the normal selections or enters the
quantities of such items as the bottles of soda, bread sticks, or Buffalo wings.

Practical Learning: Introducing Check Boxes


1. Start a new Windows Application named DaniloPizza1
2. In the Solution Explorer, right-click Form1.cs and click Rename
3. Type Exercise.vb and press Enter
4. On the main menu, click Project -> Add New Item...
5. In the Templates list, click Icon File
6. Set the Name to pizza and click Add

7. Using the Erase tool , wipe its interior to erase its content

8. Use the Ellipse tool and the black color to draw an oval shape

9. Fill it up with a brown color (the color on the right side of the black)
10. Click the red color

11. Click the Airbrush tool


12. Click the option button and select the Medium brush
13. Randomly click different parts of the shape

Page | 2

14. 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
15. With still the black and the brown colors, randomly click and right-click different
points in the shape:

16. Click the white color. Right-click the yellow color. Randomly click and right-click a
few times in the shape

17. Click the brown color again and click the Line tool
18. Randomly draw horizontal, vertical, and dialog lines in the shape

19. Right-click a white section in the drawing area, position the mouse on Current Icon
Image Types, and click 16x16, 16 Colors
20. Design it as follows:

Page | 3

21. Save the icon and close it


22. Use the Icon field of the form in the Properties window to assign the pizza.ico icon to
the form
23. Design the form as follows:
Additional
Control Text Name
Properties

GroupBox Pizza Size

RadioButton Small rdoSmall


Page | 4
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

AlignText: Right
TextBox 0.00 txtTotalBread
ReadOnly: True

Label Buffalo Wings

TextBox 0 txtQtyWings AlignText: Right

TextBox 2.15 txtPriceWings AlignText: Right

AlignText: Right
TextBox 0.00 txtTotalWings
ReadOnly: True

GroupBox Toppings

CheckBox Pepperoni chkPepperoni

CheckBox Sausage chkSausage

CheckBox Extra Cheese chkExtraCheese


CheckBox Olives chkOlives

CheckBox Onions chkOnions

Label Each Topping


Page | 5
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

AlignText: Right
TextBox 0.00 txtTotalCan
ReadOnly: True

Label Soda 20 Oz.

TextBox 0 txtQtySoda20 AlignText: Right

TextBox 1.45 txtPriceSoda20 AlignText: Right

AlignText: Right
TextBox 0.00 txtTotalSoda20
ReadOnly: True

Label Soda 2L Bottle

TextBox 0 txtQtySoda2L AlignText: Right

TextBox 1.45 txtPriceSoda2L AlignText: Right

AlignText: Right
TextBox 0.00 txtTotalSoda2L
ReadOnly: True

Label Orange Juice

TextBox 0 txtQtyOJ AlignText: Right

TextBox 2.25 txtPriceOJ AlignText: Right

AlignText: Right
TextBox 0.00 txtTotalOJ
ReadOnly: True
Label Water

TextBox 0 txtQtyWater AlignText: Right

TextBox 1.25 txtPriceWater AlignText: Right


Page | 6
AlignText: Right
TextBox 0.00 txtTotalWater
ReadOnly: True

Button Close btnClose

Label Total Price

AlignRight: Right
TextBox 0.00 txtTotalPrice
ReadOnly: True

24. Save everything


25. Right-click the form and click View Code
26. 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 Page | 7
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

27. 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, Page | 8
chkOnions.Click,
chkPepperoni.Click,
chkSausage.Click

CalculatePrice()
End Sub

28. 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 Page | 9
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
Page | 10
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 Page | 11
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

29. In the Class Name combo box, select btnClose


30. 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
31. Execute the application and test the form. Here is an example:

Page | 12

32. Close the form and return to your programming environment

Das könnte Ihnen auch gefallen