Visual Basic .NET Examples: Georgetown Cleaning Services

 

Introduction

Some times if you are asked to create an application for a store that sells a specific number of items based on a fixed list, you may ask for the list of items so you can create its elements in the application. Here is an example:
 

For such an application, when the list of items changes, you may have to modify the application, recompile, and redistribute (or re-install it). One solution to this problem would consist of providing placeholders for an employee to fill out and process the order with them. Another problem that can be common with this type of application is that, while the form may be crowded with too many objects, most of the time, some items are rarely ordered.

In this exercise, to make this type of application a little more flexible, we will use a few combo boxes that allow the user to simply select the items that are valid for the order.

Practical Learning Practical Learning: Introducing Buttons

  1. Start a new Windows Forms Application named GCS2
  2. Set the form's icon to Drive:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Misc\BULLSEYE.ICO
  3. On the main menu, click File -> New -> New File...
  4. In the New File dialog box, click Icon File (.ico) and click Open
  5. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 16x16, 16 Colors
  6. Design it as follows:
     
    Calculator - Icon Design
  7. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 32x32, 16 Colors
  8. Right-click a white area in the drawing section and click Delete Image Type
  9. On the main menu, click File -> Save Cursor1
  10. Locate the GCS2 folder that contains the current project and display it in the Save In combo box
  11. Double-click its bin folder to display in the Save In combo box
  12. Change the filename to Calculate.ico
  13. Click Save
  14. On the main menu, click File -> New -> New File...
  15. In the New File dialog box, double-click Icon File (.ico)
  16. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 16x16, 16 Colors
  17. Design the icon as follows:
     
    SubTotal - Icon Design
  18. Right-click a white area in the drawing section, position the mouse on Current Icon Image Types, and click 32x32, 16 Colors
  19. Right-click a white area in the drawing section and click Delete Image Type
  20. On the main menu, click File -> Save Cursor2
  21. Make sure the bin sub-folder of the GCS2 folder that contains the current project is selected and displays in the Save In combo box
  22. Change the filename to SubTotal.ico
  23. Click Save
  24. Return to the form (Form1.vb [Design]) and design it as follows:
     
    Control Name Text Additional Properties
    GroupBox GroupBox   Order Identification  
    Label Label   Customer Name:  
    TextBox TextBox txtCustomerName    
    Label Label   Date Left:  
    DateTimePicker DateTimePicker dtpDateLeft   Format: Custom
    Custom Format: dddd dd MMM yyyy
    Label Label   Time Left:  
    DateTimePicker DateTimePicker dtpTimeLeft   Format: Time
    Label Label   Date Expected:  
    DateTimePicker DateTimePicker dtpDateExpected   Format: Custom
    Custom Format: dddd dd MMM yyyy
    Label     Time Expected:  
    DateTimePicker DateTimePicker dtpTimeExpected   Format: Time
    GroupBox GroupBox   Order Processing  
    Label Label   Item Type  
    Label Label   Unit Price  
    Label Label   Qty  
    Label Label   Sub Total  
    Label Label   Shirts  
    TextBox TextBox txtShirtsUnitPrice 0.95 TextAlign: Right
    TextBox TextBox txtShirtsQuantity 0 TextAlign: Right
    Button Button btnCalcShirts   Image: SubTotal.ico
    TextBox TextBox txtShirtsSubTotal 0.00 TextAlign: Right
    Label Label   Pants  
    TextBox TextBox txtPantsUnitPrice 2.75 TextAlign: Right
    TextBox TextBox txtPantsQuantity   TextAlign: Right
    Button Button btnCalcPants   Image: SubTotal.ico
    TextBox TextBox txtPantsSubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem1 None  
    TextBox TextBox txtItem1UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem1Quantity 0 TextAlign: Right
    Button Button btnCalcItem1   Image: SubTotal.ico
    TextBox TextBox txtItem1SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem2 None  
    TextBox TextBox txtItem2UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem2Quantity 0 TextAlign: Right
    Button Button btnCalcItem2   Image: SubTotal.ico
    TextBox TextBox txtItem2SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem3 None  
    TextBox TextBox txtItem3UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem3Quantity 0 TextAlign: Right
    Button Button btnCalcItem3   Image: SubTotal.ico
    TextBox TextBox txtItem3SubTotal 0.00 TextAlign: Right
    ComboBox ComboBox cboItem4 None  
    TextBox TextBox txtItem4UnitPrice 0.00 TextAlign: Right
    TextBox TextBox txtItem4Quantity 0 TextAlign: Right
    Button Button btnCalcItem4   Image: SubTotal.ico
    TextBox TextBox txtItem4UnitPrice 0.00 TextAlign: Right
    Button Button btnCalculate Calculate  
    GroupBox GroupBox   Order Summary  
    Label Label   Order Total:  
    TextBox TextBox txtOrderTotal 0.00 TextAlign: Right
    Label Label   TaxRate  
    TextBox TextBox txtTaxRate 5.75 TextAlign: Right
    Label Label   %  
    Label Label   Tax Amount  
    TextBox TextBox txtTaxAmount 0.00 TextAlign: Right
    Label Label   Order Price  
    TextBox TextBox txtOrderPrice 0.00 TextAlign: Right
  25. Click each combo box. Access its Items property and fill it up as follows:
     
  26. Click OK and save All

 

Application Implementation

As a dry cleaning applications, there are some behaviors we will apply. About dates, if a customer leaves his or her clothes in the morning before 9AM, we will promise that the clothes can be ready on the same day after 5PM. If a customer leaves the clothes after 9AM, they will be ready only the following day.

To process an order, we provided the clerk with 4 combo boxes to select some of the most regularly ordered cleaning items. The combo boxes also allow the user to type an item that is not in the list. Also, because we can't or we won't predict the prices of these items, we let the user specify their price.

Once an order is ready, we will calculate it using classic operations.

Practical Learning Practical Learning: Implementing the Application

  1. On the form, double-click the Time Left control and implement its ValueChanged event as follows:
     
    Private Sub dtpDateLeft_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpDateLeft.ValueChanged
            Dim dateLeft As DateTime = Me.dtpDateLeft.Value
            Dim timeLeft As DateTime = Me.dtpTimeLeft.Value
    
            Dim time9AM As DateTime = New DateTime(timeLeft.Year, timeLeft.Month, timeLeft.Day, 9, 0, 0)
    
            ' If the customer leaves clothes before 9AM...
            If timeLeft <= time9AM Then
                ' ... then they should be ready the same day after 5PM
                Me.dtpDateExpected.Value = dateLeft
                Me.dtpTimeExpected.Value = New DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day, 17, 0, 0)
    
            Else
                ' If the clothese were left after 9AM, they will be availablethe following morning at 8AM
                Me.dtpDateExpected.Value = New DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1)
                Me.dtpTimeExpected.Value = New DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1, 8, 0, 0)
            End If
        End Sub
  2. In the Class Name combo box, select btnCalcShirts
  3. In the Method Name combo box, select Click
  4. Implement its Click event as follows:
     
    Private Sub btnCalcShirts_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalcShirts.Click
            Dim quantity As Integer = 1
            Dim unitPrice As Double
            Dim subTotal As Double
    
            ' Retrieve the number of this item
            ' Just in case the user types an invalid value, we are using a try...catch
            Try
                quantity = CInt(Me.txtShirtsQuantity.Text)
            Catch ex As FormatException
                MsgBox("The value you entered for the number of shirts is not valid" & _
                               vbCrLf & "Please try again")
            End Try
    
            ' Retrieve the unit price of this item
            ' Just in case the user types an invalid value, we are using a try...catch
            Try
                unitPrice = CDbl(Me.txtShirtsUnitPrice.Text)
            Catch ex As FormatException
                MsgBox("The value you entered for the price of shirts is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            ' Calculate the sub-total for this item
            subTotal = quantity * unitPrice
    
            ' Display the sub-total in the corresponding text box
            Me.txtShirtsSubTotal.Text = subTotal.ToString("F")
    End Sub
  5. In the Class Name combo box, select btnCalcPants
  6. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCalcPants_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalcPants.Click
            Dim quantity As Integer = 1
            Dim unitPrice As Double
            Dim subTotal As Double
    
            Try
                quantity = CInt(Me.txtPantsQuantity.Text)
            Catch ex As FormatException
                MsgBox("The value you entered for the number of pants is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            Try
                unitPrice = CDbl(Me.txtPantsUnitPrice.Text)
            Catch ex As FormatException
                MsgBox("The value you entered for the price of pants is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            subTotal = quantity * unitPrice
    
            Me.txtPantsSubTotal.Text = subTotal.ToString("F")
    End Sub
  7. In the Class Name combo box, select btnCalcItem1
  8. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCalcItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalcItem1.Click
            Dim quantity As Integer = 1
            Dim unitPrice As Double
            Dim subTotal As Double
    
            Try
                quantity = CInt(Me.txtItem1Quantity.Text)
            Catch ex As FormatException
                MsgBox("The value you entered is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            Try
                unitPrice = CDbl(Me.txtItem1UnitPrice.Text)
            Catch ex As FormatException
                MsgBox("The value you entered for the price is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            subTotal = quantity * unitPrice
    
            Me.txtItem1SubTotal.Text = subTotal.ToString("F")
    End Sub
  9. In the Class Name combo box, select btnCalcItem2
  10. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCalcItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalcItem2.Click
            Dim quantity As Integer = 1
            Dim unitPrice As Double
            Dim subTotal As Double
    
            Try
                quantity = CInt(Me.txtItem2Quantity.Text)
            Catch ex As FormatException
                MsgBox("The value you entered is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            Try
                unitPrice = CDbl(Me.txtItem2UnitPrice.Text)
            Catch ex As FormatException
                MsgBox("The value you entered for the price is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            subTotal = quantity * unitPrice
    
            Me.txtItem2SubTotal.Text = subTotal.ToString("F")
    End Sub
  11. In the Class Name combo box, select btnCalcItem3
  12. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCalcItem3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalcItem3.Click
            Dim quantity As Integer = 1
            Dim unitPrice As Double
            Dim subTotal As Double
    
            Try
                quantity = CInt(Me.txtItem3Quantity.Text)
            Catch ex As FormatException
                MsgBox("The value you entered is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            Try
    		unitPrice = CDbl(Me.txtItem3UnitPrice.Text.)
            Catch ex As FormatException
    
                MsgBox("The value you entered for the price is not valid" & _
                    vbCrLf & "Please try again")
            End Try
    
            subTotal = quantity * unitPrice
            Me.txtItem3SubTotal.Text = subTotal.ToString("F")
    End Sub
  13. In the Class Name combo box, select btnCalcItem4
  14. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCalcItem4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalcItem4.Click
            Dim quantity As Integer = 1
            Dim unitPrice As Double
            Dim subTotal As Double
    
            Try
                quantity = CInt(Me.txtItem4Quantity.Text)
            Catch ex As FormatException
                MsgBox("The value you entered is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            Try
                unitPrice = CDbl(Me.txtItem4UnitPrice.Text)
            Catch ex As FormatException
                MsgBox("The value you entered for the price is not valid" & _
                              vbCrLf & "Please try again")
            End Try
    
            subTotal = quantity * unitPrice
            Me.txtItem4SubTotal.Text = subTotal.ToString("F")
    End Sub
  15. In the Class Name combo box, select btnCalculate button
  16. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim priceShirts As Double
            Dim pricePants As Double
            Dim priceItem1 As Double
            Dim priceItem2 As Double
            Dim priceItem3 As Double
            Dim priceItem4 As Double
            Dim totalOrder As Double
            Dim taxRate As Double
            Dim taxAmount As Double
            Dim netPrice As Double
    
            ' Retrieve the value of the sub-total for each category of items
            priceShirts = CDbl(Me.txtShirtsSubTotal.Text)
            pricePants = CDbl(Me.txtPantsSubTotal.Text)
            priceItem1 = CDbl(Me.txtItem1SubTotal.Text)
            priceItem2 = CDbl(Me.txtItem2SubTotal.Text)
            priceItem3 = CDbl(Me.txtItem3SubTotal.Text)
            priceItem4 = CDbl(Me.txtItem4SubTotal.Text)
    
            ' Calculate the total
            totalOrder = priceShirts + pricePants + _
               priceItem1 + priceItem2 + priceItem3 + priceItem4
    
            ' Retrieve the value of the tax rate
            Try
                taxRate = CDbl(Me.txtTaxRate.Text)
            Catch ex As FormatException
                MsgBox("The tax rate you entered is invalid" & _
                              vbCrLf & "Please try again")
            End Try
    
            ' Calculate the amount owed for the taxes
            taxAmount = totalOrder * taxRate / 100
            ' Add the tax amount to the total order
            netPrice = totalOrder + taxAmount
    
            ' Display the values of the order summary
            Me.txtOrderTotal.Text = totalOrder.ToString("C")
            Me.txtTaxAmount.Text = taxAmount.ToString("C")
            Me.txtOrderPrice.Text = netPrice.ToString("C")
    End Sub
  17. Execute the application to test the form
  18. Close the form and return to your programming environment
 

Home Copyright © 2004-2011 FunctionX