- Right-click the form and click View Code
- Define the following procedure just under #End Region:
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 As Double
Dim 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
' 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
' 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
PriceEachTopping = CDbl(txtEachTopping.Text)
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
txtTotalPrice.Text = FormatCurrency(TotalOrder)
End Sub
|
- On the form double-click each radio button and each check box
- In their implementation, simply call the CalculatePrice() function:
Private Sub rdoSmall_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoSmall.CheckedChanged
CalculatePrice()
End Sub
Private Sub rdoMedium_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoMedium.CheckedChanged
CalculatePrice()
End Sub
Private Sub rdoLarge_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoLarge.CheckedChanged
CalculatePrice()
End Sub
Private Sub chkPepperoni_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPepperoni.CheckedChanged
CalculatePrice()
End Sub
Private Sub chkSausage_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSausage.CheckedChanged
CalculatePrice()
End Sub
Private Sub chkExtraCheese_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkExtraCheese.CheckedChanged
CalculatePrice()
End Sub
Private Sub chkOlives_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOlives.CheckedChanged
CalculatePrice()
End Sub
Private Sub chkOnions_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOnions.CheckedChanged
CalculatePrice()
End Sub
|
- Test the application and close the form
- In the Objects combo box, select the txtQtyBread
- In the Procedure combo box, select Leave
- In the same way, initiate the Leave event of each Qty text box
- Implement the events as follows:
Private Sub txtQtyBread_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQtyBread.Leave
Dim Qty As Integer
Dim Price As Double
Dim Total As Double
If txtQtyBread.Text = "" Then
Qty = 0
Else
Qty = CInt(txtQtyBread.Text)
End If
Price = CDbl(txtPriceBread.Text)
Total = Qty * Price
txtTotalBread.Text = FormatCurrency(Total)
CalculatePrice()
End Sub
Private Sub txtQtyCan_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQtyCan.Leave
Dim Qty As Integer
Dim Price As Double
Dim Total As Double
If txtQtyCan.Text = "" Then
Qty = 0
Else
Qty = CInt(txtQtyCan.Text)
End If
Price = CDbl(txtPriceCan.Text)
Total = Qty * Price
txtTotalCan.Text = FormatCurrency(Total)
CalculatePrice()
End Sub
Private Sub txtQtyOJ_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQtyOJ.Leave
Dim Qty As Integer
Dim Price As Double
Dim Total As Double
If txtQtyOJ.Text = "" Then
Qty = 0
Else
Qty = CInt(txtQtyOJ.Text)
End If
Price = CDbl(txtPriceOJ.Text)
Total = Qty * Price
txtTotalOJ.Text = FormatCurrency(Total)
CalculatePrice()
End Sub
Private Sub txtQtySoda20_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQtySoda20.Leave
Dim Qty As Integer
Dim Price As Double
Dim Total As Double
If txtQtySoda20.Text = "" Then
Qty = 0
Else
Qty = CInt(txtQtySoda20.Text)
End If
Price = CDbl(txtPriceSoda20.Text)
Total = Qty * Price
txtTotalSoda20.Text = FormatCurrency(Total)
CalculatePrice()
End Sub
Private Sub txtQtySoda2L_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQtySoda2L.Leave
Dim Qty As Integer
Dim Price As Double
Dim Total As Double
If txtQtySoda2L.Text = "" Then
Qty = 0
Else
Qty = CInt(txtQtySoda2L.Text)
End If
Price = CDbl(txtPriceSoda2L.Text)
Total = Qty * Price
txtTotalSoda2L.Text = FormatCurrency(Total)
CalculatePrice()
End Sub
Private Sub txtQtyWater_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQtyWater.Leave
Dim Qty As Integer
Dim Price As Double
Dim Total As Double
If txtQtyWater.Text = "" Then
Qty = 0
Else
Qty = CInt(txtQtyWater.Text)
End If
Price = CDbl(txtPriceWater.Text)
Total = Qty * Price
txtTotalWater.Text = FormatCurrency(Total)
CalculatePrice()
End Sub
Private Sub txtQtyWings_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQtyWings.Leave
Dim Qty As Integer
Dim Price As Double
Dim Total As Double
If txtQtyWings.Text = "" Then
Qty = 0
Else
Qty = CInt(txtQtyWings.Text)
End If
Price = CDbl(txtPriceWings.Text)
Total = Qty * Price
txtTotalWings.Text = FormatCurrency(Total)
CalculatePrice()
End Sub
|
- On the form, double-click the Close button and implement its Click
event as follows:
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Close()
End Sub
|
- Test the application
|