Danilo Pizza

Creating the Application

This application is used to process orders for a pizza business. It will allow us to view examples of validating radio buttons and check boxes

Prerequisites:

  • Radio Buttons
  • Check Boxes
  • Dialog Boxes

This is a classic application that is created from a dialog box.

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual Basic .Net and create a new Windows Application named Danilo Pizza
     
  2. 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
    Label Buffalo Wings    
    TextBox 0 txtQtyWings AlignText: Right
    TextBox 2.15 txtPriceWings AlignText: Right
    TextBox 0.00 txtTotalWings AlignText: Right
    GroupBox Toppings    
    CheckBox Pepperoni chkPepperoni CheckAlign: MiddleRight
    CheckBox Sausage chkSausage CheckAlign: MiddleRight
    CheckBox Extra Cheese chkExtraCheese CheckAlign: MiddleRight
    CheckBox Olives chkOlives CheckAlign: MiddleRight
    CheckBox Onions chkOnions CheckAlign: MiddleRight
    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
    Label Soda 20 Oz.    
    TextBox 0 txtQtySoda20 AlignText: Right
    TextBox 1.45 txtPriceSoda20 AlignText: Right
    TextBox 0.00 txtTotalSoda20 AlignText: Right
    Label Soda 2L Bottle    
    TextBox 0 txtQtySoda2L AlignText: Right
    TextBox 1.45 txtPriceSoda2L AlignText: Right
    TextBox 0.00 txtTotalSoda2L AlignText: Right
    Label Orange Juice    
    TextBox 0 txtQtyOJ AlignText: Right
    TextBox 2.25 txtPriceOJ AlignText: Right
    TextBox 0.00 txtTotalOJ AlignText: Right
    Label Water    
    TextBox 0 txtQtyWater AlignText: Right
    TextBox 1.25 txtPriceWater AlignText: Right
    TextBox 0.00 txtTotalWater AlignText: Right
    Button Close btnClose  
    Label Total Price    
    Text Box 0.00 txtTotalPrice AlignRight: Right
  3. Save everything
 

Coding the Application

  1. Right-click the form and click View Code
  2. 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
  3. On the form double-click each radio button and each check box
  4. 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
  5. Test the application and close the form
  6. In the Objects combo box, select the txtQtyBread
  7. In the Procedure combo box, select Leave
  8. In the same way, initiate the Leave event of each Qty text box
  9. 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
  10. 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
  11. Test the application
 
 
 

Home Copyright 2004-2005 FunctionX, Inc.