Danilo Pizza

Introduction

In this example, we will create an order processing application for a pizza restaurant.

  1. Start Microsoft Visual Basic and create a Standard EXE application
  2. Save the application in a new folder named Danilo Pizza
  3. Save the form as Main and save the project as DaniloPizza
  4. Design the form as follows
     
     
    Control Caption or Text Name Additional Properties
    Frame Pizza Size    
    OptionButton Small optSmall  
    TextBox 8.95 txtSmall Alignment: 1 - Right Justify
    OptionButton Medium optMedium Value: True
    TextBox 10.75 txtMedium Alignment: 1 - Right Justify
    OptionButton Large optLarge  
    TextBox 12.95 txtLarge Alignment: 1 - Right Justify
    Frame Side Orders    
    Label Qty    
    Label Unit Price   Alignment: 1 - Right Justify
    Label Sub Total    
    Label Buffalo Wings    
    TextBox 0 txtQtyWings Alignment: 1 - Right Justify
    TextBox 3.25 txtPriceWings Alignment: 1 - Right Justify
    TextBox 0.00 txtTotalWings Alignment: 1 - Right Justify
    Label Bread Sticks    
    TextBox 0 txtQtyBread Alignment: 1 - Right Justify
    TextBox 2.15 txtPriceBread Alignment: 1 - Right Justify
    TextBox 0.00 txtTotalBread Alignment: 1 - Right Justify
    Frame Toppings    
    CheckBox Pepperoni chkPepperoni Alignment: 1 - Right Justify
    CheckBox Sausage chkSausage Alignment: 1 - Right Justify
    CheckBox Extra Cheese chkExtraCheese Alignment: 1 - Right Justify
    CheckBox Olives chkOlives Alignment: 1 - Right Justify
    CheckBox Onions chkOnions Alignment: 1 - Right Justify
    Label Each Topping    
    TextBox 0.45 txtEachTopping Alignment: 1 - Right Justify
    Frame Drinks    
    Label Qty    
    Label Unit Price    
    Label Sub Total    
    Label Soda Can    
    TextBox 0 txtQtyCan Alignment: 1 - Right Justify
    TextBox 1.45 txtPriceCan Alignment: 1 - Right Justify
    TextBox 0.00 txtTotalCan Alignment: 1 - Right Justify
    Label Soda 20 Oz.    
    TextBox 0 txtQtySoda20 Alignment: 1 - Right Justify
    TextBox 1.45 txtPriceSoda20 Alignment: 1 - Right Justify
    TextBox 0.00 txtTotalSoda20 Alignment: 1 - Right Justify
    Label Soda 2L Bottle    
    TextBox 0 txtQtySoda2L Alignment: 1 - Right Justify
    TextBox 1.45 txtPriceSoda2L Alignment: 1 - Right Justify
    TextBox 0.00 txtTotalSoda2L Alignment: 1 - Right Justify
    Label Orange Juice    
    TextBox 0 txtQtyOJ Alignment: 1 - Right Justify
    TextBox 2.25 txtPriceOJ Alignment: 1 - Right Justify
    TextBox 0.00 txtTotalOJ Alignment: 1 - Right Justify
    Label Water    
    TextBox 0 txtQtyWater Alignment: 1 - Right Justify
    TextBox 1.25 txtPriceWater Alignment: 1 - Right Justify
    TextBox 0.00 txtTotalWater Alignment: 1 - Right Justify
    Button Close cmdClose  
    Label Total Price    
    Text Box 0.00 txtTotalPrice Alignment: 1 - Right Justify

     

  5. Right-click the form and click View Code
  6. Define the following procedure:
     
    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 optSmall.Value = True Then
            PriceSize = CDbl(txtSmall.Text)
        End If
        If optMedium.Value = True Then
            PriceSize = CDbl(txtMedium.Text)
        End If
        If optLarge.Value = True Then
            PriceSize = CDbl(txtLarge.Text)
        End If
    
        ' Get the price of a topping if it was selected
        If chkPepperoni = 1 Then
            Pepperoni = 1
        Else
            Pepperoni = 0
        End If
            
        If chkSausage = 1 Then
            Sausage = 1
        Else
            Sausage = 0
        End If
            
        If chkExtraCheese = 1 Then
            ExtraCheese = 1
        Else
            ExtraCheese = 0
        End If
            
        If chkOnions = 1 Then
            Onions = 1
        Else
            Onions = 0
        End If
    
        If chkOlives = 1 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
  7. On the form double-click each radio button and each check box
  8. In their implementation, simply call the CalculatePrice() function:
     
    Private Sub chkExtraCheese_Click()
        CalculatePrice
    End Sub
    
    Private Sub chkOlives_Click()
        CalculatePrice
    End Sub
    
    Private Sub chkOnions_Click()
        CalculatePrice
    End Sub
    
    Private Sub chkPepperoni_Click()
        CalculatePrice
    End Sub
    
    Private Sub chkSausage_Click()
        CalculatePrice
    End Sub
    
    Private Sub optSmall_Click()
        CalculatePrice
    End Sub
    
    Private Sub optMedium_Click()
        CalculatePrice
    End Sub
    
    Private Sub optLarge_Click()
        CalculatePrice
    End Sub
    
    Private Sub txtLarge_Change()
        CalculatePrice
    End Sub
    
    Private Sub txtMedium_Change()
        CalculatePrice
    End Sub
  9. Test the application and close the form
  10. In the Objects combo box, select the txtQtyWings
  11. In the Procedure combo box, select LostFocus
  12. In the same way, initiate the LostFocus event of each Qty text box
  13. Implement the events as follows:
     
    Private Sub txtQtyBread_LostFocus()
        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_LostFocus()
        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_LostFocus()
        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_LostFocus()
        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_LostFocus()
        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_LostFocus()
        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_LostFocus()
        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
  14. On the form, double-click the Close button and implement its Click event as follows:
     
    Private Sub cmdClose_Click()
        End
    End Sub
  15. Test the application
 
 

Home Copyright 2004 FunctionX, Inc.