Home

MS Visual Basic Example Application: College Park Auto Repair

 

Introduction

This application is an example of creating and managing XML elements using the XmlTextWriter and the XmlTextReader classes of the .NET Framework, which implement the processes of writing to, and reading from, an XML file. The application is used by a car repair shop that takes orders and processes them.

Windows Controls Used:

 

Practical LearningPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio .NET and create a Windows Forms Application named CollegeParkAutoRepair1
  2. In the Solution Explorer, right-click Form1.vb and click Rename
  3. Type CPAR.vb and press Enter
  4. From the Menus & Toolbars section of the Toolbox, click MenuStrip and click the form
  5. Design the menu items as follows:
     
    MenuItem DropDownItems
    Text Name Text Name Shortcut
    &File mnuFile &New Repair Order mnuFileNew Ctrl+N
        &Open Existing Order... mnuFileOpen Ctrl+O
        &Save Current Order mnuFileSave Ctrl+S
        Separator    
        &Print... mnuFilePrint Ctrl+P
        Print Pre&view... mnuFilePrintPreview  
        Separator    
        E&xit mnuFileExit  
  6. Design the form as follows:
     
    College Park Auto-Repair
    Control Name Text Other Properties
    Group   Order Identification  
    Label   Customer Name:  
    TextBox txtCustomerName    
    Label   Address  
    TextBox txtAddress    
    Label   City:  
    TextBox txtCity    
    Label   State:  
    TextBox txtState    
    Label   ZIP Code:  
    TextBox txtZIPCode   TextAlign: Right
    Label   Make / Model:  
    TextBox txtMake    
    TextBox txtModel    
    Label   Year:  
    TextBox txtCarYear   TextAlign: Right
    Label   Problem Description:  
    TextBox txtProblem    
    GroupBox   Parts Used  
    Label   Part Name  
    Label   Unit Price  
    Label   Qty  
    Label   Sub Total  
    TextBox txtPartName1    
    TextBox txtUnitPrice1 0.00 TextAlign: Right
    TextBox txtQuantity1 0 TextAlign: Right
    TextBox txtSubTotal1 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName2    
    TextBox txtUnitPrice2 0.00 TextAlign: Right
    TextBox txtQuantity2 0 TextAlign: Right
    TextBox txtSubTotal2 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName3    
    TextBox txtUnitPrice3 0.00 TextAlign: Right
    TextBox txtQuantity3 0 TextAlign: Right
    TextBox txtSubTotal3 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName4    
    TextBox txtUnitPrice4 0.00 TextAlign: Right
    TextBox txtQuantity4 0 TextAlign: Right
    TextBox txtSubTotal4 0.00 TextAlign: Right
    Enabled: False
    TextBox txtPartName5    
    TextBox txtUnitPrice5 0.00 TextAlign: Right
    TextBox txtQuantity5 0 TextAlign: Right
    TextBox txtSubTotal5 0.00 TextAlign: Right
    Enabled: False
    GroupBox   Jobs Performed  
    Label   Price  
    TextBox txtJobPerformed1    
    TextBox txtJobPrice1 0.00 TextAlign: Right
    TextBox txtJobPerformed2    
    TextBox txtJobPrice2 0.00 TextAlign: Right
    TextBox txtJobPerformed3    
    TextBox txtJobPrice3 0.00 TextAlign: Right
    TextBox txtJobPerformed4    
    TextBox txtJobPrice4 0.00 TextAlign: Right
    TextBox txtJobPerformed5    
    TextBox txtJobPrice5 0.00 TextAlign: Right
    GroupBox   Order Summary  
    Label   Total Parts:  
    TextBox txtTotalParts 0.00 TextAlign: Right
    Label   Total Labor:  
    TextBox txtTotalLabor 0.00 TextAlign: Right
    Label   Tax Rate:  
    TextBox txtTaxRate 7.75 TextAlign: Right
    Label   %  
    Label   Tax Amount:  
    TextBox txtTaxAmount 0.00 TextAlign: Right
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 TextAlign: Right
    Label   Recommendations  
    TextBox txtRecommendations   Multiline: True
    ScrollBars: Vertical
  7. Right-click the form and click View Code
  8. Just above the Public Class CPAR line, import the following namespaces:
     
    Imports System.IO
    Imports System.Xml
    Imports System.Text
    
    Public Class CPAR
    
    End Class
  9. Under the Public Class CPAR line, create a sub-procedure named Calculate as follows:
      
    Imports System.IO
    Imports System.Xml
    
    Public Class CPAR
        Private Sub Calculate()
            Dim UnitPricePart1 As Double, UnitPricePart2 As Double
            Dim UnitPricePart3 As Double, UnitPricePart4 As Double
            Dim UnitPricePart5 As Double
            Dim SubTotalPart1 As Double, SubTotalPart2 As Double
            Dim SubTotalPart3 As Double, SubTotalPart4 As Double
            Dim SubTotalPart5 As Double, TotalParts As Double
            Dim QuantityPart1 As Integer, QuantityPart2 As Integer
            Dim QuantityPart3 As Integer, QuantityPart4 As Integer
            Dim QuantityPart5 As Integer
            Dim Job1Price As Double, Job2Price As Double
            Dim Job3Price As Double, Job4Price As Double
            Dim Job5Price As Double, TotalLabor As Double
            Dim TaxRate As Double, TaxAmount As Double
            Dim TotalOrder As Double
    
            ' Don't charge a part unless it is clearly identified
            If txtPartName1.Text = "" Then
                txtUnitPrice1.Text = "0.00"
                txtQuantity1.Text = "0"
                txtSubTotal1.Text = "0.00"
                UnitPricePart1 = 0.0
            Else
                Try
                    UnitPricePart1 = CDbl(txtUnitPrice1.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Unit Price")
                    txtUnitPrice1.Text = "0.00"
                    txtUnitPrice1.Focus()
                End Try
    
                Try
                    QuantityPart1 = CInt(txtQuantity1.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Quantity")
                    txtQuantity1.Text = "0"
                    txtQuantity1.Focus()
                End Try
            End If
    
            If txtPartName2.Text = "" Then
                txtUnitPrice2.Text = "0.00"
                txtQuantity2.Text = "0"
                txtSubTotal2.Text = "0.00"
                UnitPricePart2 = 0.0
            Else
                Try
                    UnitPricePart2 = CDbl(txtUnitPrice2.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Unit Price")
                    txtUnitPrice2.Text = "0.00"
                    txtUnitPrice2.Focus()
                End Try
    
                Try
                    QuantityPart2 = CInt(txtQuantity2.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Quantity")
                    txtQuantity2.Text = "0"
                    txtQuantity2.Focus()
                End Try
            End If
    
            If txtPartName3.Text = "" Then
                txtUnitPrice3.Text = "0.00"
                txtQuantity3.Text = "0"
                txtSubTotal3.Text = "0.00"
                UnitPricePart3 = 0.0
            Else
                Try
                    UnitPricePart3 = CDbl(txtUnitPrice3.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Unit Price")
                    txtUnitPrice3.Text = "0.00"
                    txtUnitPrice3.Focus()
                End Try
    
                Try
                    QuantityPart3 = CInt(txtQuantity3.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Quantity")
                    txtQuantity3.Text = "0"
                    txtQuantity3.Focus()
                End Try
            End If
    
            If txtPartName4.Text = "" Then
                txtUnitPrice4.Text = "0.00"
                txtQuantity4.Text = "0"
                txtSubTotal4.Text = "0.00"
                UnitPricePart4 = 0.0
            Else
                Try
                    UnitPricePart4 = CDbl(txtUnitPrice4.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Unit Price")
                    txtUnitPrice4.Text = "0.00"
                    txtUnitPrice4.Focus()
                End Try
    
                Try
                    QuantityPart4 = CInt(txtQuantity4.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Quantity")
                    txtQuantity4.Text = "0"
                    txtQuantity4.Focus()
                End Try
            End If
    
            If txtPartName5.Text = "" Then
                txtUnitPrice5.Text = "0.00"
                txtQuantity5.Text = "0"
                txtSubTotal5.Text = "0.00"
                UnitPricePart5 = 0.0
            Else
                Try
                    UnitPricePart5 = CDbl(txtUnitPrice5.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Unit Price")
                    txtUnitPrice5.Text = "0.00"
                    txtUnitPrice5.Focus()
                End Try
    
                Try
                    QuantityPart5 = CInt(txtQuantity5.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Quantity")
                    txtQuantity5.Text = "0"
                    txtQuantity5.Focus()
                End Try
            End If
    
            ' Don't bill the customer for a job that is not specified
            If txtJobDescription1.Text = "" Then
                txtJobPrice1.Text = "0.00"
                Job1Price = 0.0
            Else
                Try
                    job1Price = CDbl(txtJobPrice1.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Job Price")
                    txtJobPrice1.Text = "0.00"
                    txtJobPrice1.Focus()
                End Try
            End If
    
            If txtJobDescription2.Text = "" Then
                txtJobPrice2.Text = "0.00"
                job2Price = 0.0
            Else
                Try
                    job2Price = CDbl(txtJobPrice2.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Job Price")
                    txtJobPrice2.Text = "0.00"
                    txtJobPrice2.Focus()
                End Try
            End If
    
            If txtJobDescription3.Text = "" Then
                txtJobPrice3.Text = "0.00"
                job3Price = 0.0
            Else
                Try
                    job3Price = CDbl(txtJobPrice3.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Job Price")
                    txtJobPrice3.Text = "0.00"
                    txtJobPrice3.Focus()
                End Try
            End If
    
            If txtJobDescription4.Text = "" Then
                txtJobPrice4.Text = "0.00"
                job4Price = 0.0
            Else
                Try
                    job4Price = CDbl(txtJobPrice4.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Job Price")
                    txtJobPrice4.Text = "0.00"
                    txtJobPrice4.Focus()
                End Try
            End If
    
            If txtJobDescription5.Text = "" Then
                txtJobPrice5.Text = "0.00"
                job5Price = 0.0
            Else
                Try
                    job5Price = CDbl(txtJobPrice5.Text)
                Catch Exc As FormatException
                    MsgBox("Invalid Job Price")
                    txtJobPrice5.Text = "0.00"
                    txtJobPrice5.Focus()
                End Try
            End If
    
            SubTotalPart1 = UnitPricePart1 * QuantityPart1
            SubTotalPart2 = UnitPricePart2 * QuantityPart2
            SubTotalPart3 = UnitPricePart3 * QuantityPart3
            SubTotalPart4 = UnitPricePart4 * QuantityPart4
            SubTotalPart5 = UnitPricePart5 * QuantityPart5
    
            txtSubTotal1.Text = FormatNumber(SubTotalPart1)
            txtSubTotal2.Text = FormatNumber(SubTotalPart2)
            txtSubTotal3.Text = FormatNumber(SubTotalPart3)
            txtSubTotal4.Text = FormatNumber(SubTotalPart4)
            txtSubTotal5.Text = FormatNumber(SubTotalPart5)
    
            TotalParts = SubTotalPart1 + SubTotalPart2 + _
                         SubTotalPart3 + SubTotalPart4 + SubTotalPart5
    
            TotalLabor = Job1Price + Job2Price + Job3Price + _
                            Job4Price + Job5Price
    
            Try
                TaxRate = CDbl(txtTaxRate.Text)
            Catch Exc As FormatException
                MsgBox("Invalid Tax Rate")
                txtTaxRate.Text = "7.75"
                txtTaxRate.Focus()
            End Try
    
            Dim totalPartsAndLabor As Double = totalParts + totalLabor
            TaxAmount = totalPartsAndLabor * TaxRate / 100
            TotalOrder = totalPartsAndLabor + TaxAmount
    
            txtTotalParts.Text = FormatNumber(TotalParts)
            txtTotalLabor.Text = FormatNumber(TotalLabor)
            txtTaxAmount.Text = FormatNumber(TaxAmount)
            txtTotalOrder.Text = FormatNumber(TotalOrder)
        End Sub
    End Class
  10. In the Class Name combo box, select mnuFileNew
  11. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuFileNew_Click(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles mnuFileNew.Click
            txtCustomerName.Text = ""
            txtAddress.Text = ""
            txtCity.Text = ""
            txtState.Text = ""
            txtZIPCode.Text = ""
            txtMake.Text = ""
            txtModel.Text = ""
            txtCarYear.Text = ""
            txtProblem.Text = ""
    
            txtPartName1.Text = ""
            txtUnitPrice1.Text = "0.00"
            txtQuantity1.Text = "0"
            txtSubTotal1.Text = "0.00"
            txtPartName2.Text = ""
            txtUnitPrice2.Text = "0.00"
            txtQuantity2.Text = "0"
            txtSubTotal2.Text = "0.00"
            txtPartName3.Text = ""
            txtUnitPrice3.Text = "0.00"
            txtQuantity3.Text = "0"
            txtSubTotal3.Text = "0.00"
            txtPartName4.Text = ""
            txtUnitPrice4.Text = "0.00"
            txtQuantity4.Text = "0"
            txtSubTotal4.Text = "0.00"
            txtPartName5.Text = ""
            txtUnitPrice5.Text = "0.00"
            txtQuantity5.Text = "0"
            txtSubTotal5.Text = "0.00"
    
            txtJobDescription1.Text = ""
            txtJobPrice1.Text = "0.00"
            txtJobDescription2.Text = ""
            txtJobPrice2.Text = "0.00"
            txtJobDescription3.Text = ""
            txtJobPrice3.Text = "0.00"
            txtJobDescription4.Text = ""
            txtJobPrice4.Text = "0.00"
            txtJobDescription5.Text = ""
            txtJobPrice5.Text = "0.00"
    
            txtTotalParts.Text = "0.00"
            txtTotalLabor.Text = "0.00"
            txtTaxRate.Text = "7.75"
            txtTaxAmount.Text = "0.00"
            txtTotalOrder.Text = "0.00"
    
            txtRecommendations.Text = ""
            txtCustomerName.Focus()
    End Sub
  12. Under the above End Sub line, create the following common eventand click the first text box under Unit Price
  13. In the Properties window, click the Events button and double-click Leave
  14. Call the Calculate() method as follows:
     
    Private Sub ControlsLeave(ByVal sender As Object, _
                                  ByVal e As EventArgs) _
                                  Handles txtUnitPrice1.Leave, _
                                          txtUnitPrice2.Leave, _
                                          txtUnitPrice3.Leave, _
                                          txtUnitPrice4.Leave, _
                                          txtUnitPrice5.Leave, _
                                          txtQuantity1.Leave, _
                                          txtQuantity2.Leave, _
                                          txtQuantity3.Leave, _
                                          txtQuantity4.Leave, _
                                          txtQuantity5.Leave, _
                                          txtJobPrice1.Leave, _
                                          txtJobPrice2.Leave, _
                                          txtJobPrice3.Leave, _
                                          txtJobPrice4.Leave, _
                                          txtJobPrice5.Leave, _
                                          txtTaxRate.Leave
            Calculate()
    End Sub
  15. Return to the form
  16. From the Printing section of the Toolbox, click the PrintDocument button PrintDocument and click the form
  17. While the print document control is still selected, in the Properties window, change its name to docPrint
  18. Right-click the form and click View Code
  19. In the Class name combo box, select docPrint
  20. In the Method Name combo box, select PrintPage and implement the event as follows:
     
    Private Sub docPrint_PrintPage(ByVal sender As Object, _
                   ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
                                       Handles docPrint.PrintPage
        e.Graphics.DrawLine(New Pen(Color.Black, 2), 60, 90, 680, 90)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), 60, 93, 680, 93)
    
        Dim strDisplay As String = "College Park Auto Repair"
        Dim fntString As System.Drawing.Font = _
    		New Font("Times New Roman", 28, _
        		         FontStyle.Bold)
        e.Graphics.DrawString(strDisplay, fntString, _
                        Brushes.Black, 160, 100)
    
        strDisplay = "Customer Car Repair Order"
        fntString = New System.Drawing.Font("Times New Roman", 18, _
                        FontStyle.Bold)
        e.Graphics.DrawString(strDisplay, fntString, _
                    Brushes.Black, 220, 150)
    
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
        			60, 184, 680, 184)
        e.Graphics.DrawLine(New Pen(Color.Black, 2), _
    			60, 187, 680, 187)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    					12, FontStyle.Bold)
        e.Graphics.DrawString("Order Identification", fntString, _
        		          Brushes.Black, 80, 200)
    
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 250, 640, 250)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    					10, FontStyle.Bold)
        e.Graphics.DrawString("Customer Name:", fntString, _
        			  Brushes.Black, 100, 260)
        fntString = New System.Drawing.Font("Times New Roman", 10, _
        			                FontStyle.Regular)
        e.Graphics.DrawString(txtCustomerName.Text, fntString, _
        	                  Brushes.Black, 260, 260)
    
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 280, 640, 280)
    
        fntString = New Font("Times New Roman", 10, FontStyle.Bold)
        e.Graphics.DrawString("Address:", fntString, _
    		            Brushes.Black, 100, 290)
        fntString = New Font("Times New Roman", _
     			 10, FontStyle.Regular)
        e.Graphics.DrawString(txtAddress.Text, fntString, _
        		          Brushes.Black, 260, 290)
    
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 310, 640, 310)
    
        fntString = New Font("Times New Roman", _
    			 10, FontStyle.Regular)
        Dim strAddress As String = txtCity.Text.ToString()& ", " & _
        			    txtState.Text(+" " & txtZIPCode.Text)
        e.Graphics.DrawString(strAddress, fntString, _
     			  Brushes.Black, 260, 320)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 340, 640, 340)
    
        fntString = New Font("Times New Roman", 10, FontStyle.Bold)
        e.Graphics.DrawString("Car:", fntString, _
    			  Brushes.Black, 100, 350)
        fntString = New Font("Times New Roman", _
    			 10, FontStyle.Regular)
        Dim strCar As String = txtMake.Text & ", " & txtModel.Text & _
                                ", " & txtCarYear.Text
        e.Graphics.DrawString(strCar, fntString, _
      			  Brushes.Black, 260, 350)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 370, 640, 370)
    
        fntString = New Font("Times New Roman", 10, FontStyle.Bold)
        e.Graphics.DrawString("Problem Description:", fntString, _
                    Brushes.Black, 100, 380)
        fntString = New Font("Times New Roman", _
    			 10, FontStyle.Regular)
        e.Graphics.DrawString(txtProblem.Text, fntString, _
                                  Brushes.Black, _
                                  New RectangleF(260, 380, 420, 380))
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 400, 640, 400)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    					12, FontStyle.Bold)
        e.Graphics.DrawString("Parts Used", fntString, _
        		            Brushes.Black, 80, 430)
    
        e.Graphics.DrawLine(New Pen(Color.Black, 2), _
    			80, 450, 680, 450)
    
        e.Graphics.DrawString("Parts Name", fntString, _
    		            Brushes.Black, 100, 460)
        e.Graphics.DrawString("Unit Price", fntString, _
        		          Brushes.Black, 420, 460)
        e.Graphics.DrawString("Qty", fntString, _
        		          Brushes.Black, 520, 460)
        e.Graphics.DrawString("Sub-Total", fntString, _
        		          Brushes.Black, 562, 460)
    
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 480, 640, 480)
    
        fntString = New Font("Times New Roman", 10, _
    			 FontStyle.Regular)
        Dim fmtString As StringFormat = New StringFormat
        fmtString.Alignment = StringAlignment.Far
    
        e.Graphics.DrawString(txtPartName1.Text, fntString, _
        		          Brushes.Black, 100, 490)
        e.Graphics.DrawString(txtUnitPrice1.Text, fntString, _
        		        Brushes.Black, 480, 490, fmtString)
        e.Graphics.DrawString(txtQuantity1.Text, fntString, _
        		        Brushes.Black, 540, 490, fmtString)
        e.Graphics.DrawString(txtSubTotal1.Text, fntString, _
        		        Brushes.Black, 630, 490, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
        		            100, 510, 640, 510)
    
        e.Graphics.DrawString(txtPartName2.Text, fntString, _
                Brushes.Black, 100, 520)
        e.Graphics.DrawString(txtUnitPrice2.Text, fntString, _
                Brushes.Black, 480, 520, fmtString)
        e.Graphics.DrawString(txtQuantity2.Text, fntString, _
                Brushes.Black, 540, 520, fmtString)
        e.Graphics.DrawString(txtSubTotal2.Text, fntString, _
                Brushes.Black, 630, 520, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 540, 640, 540)
    
        e.Graphics.DrawString(txtPartName3.Text, fntString, _
                Brushes.Black, 100, 550)
        e.Graphics.DrawString(txtUnitPrice3.Text, fntString, _
                Brushes.Black, 480, 550, fmtString)
        e.Graphics.DrawString(txtQuantity3.Text, fntString, _
                Brushes.Black, 540, 550, fmtString)
        e.Graphics.DrawString(txtSubTotal3.Text, fntString, _
                Brushes.Black, 630, 550, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
                    100, 570, 640, 570)
    
        e.Graphics.DrawString(txtPartName4.Text, fntString, _
                Brushes.Black, 100, 580)
        e.Graphics.DrawString(txtUnitPrice4.Text, fntString, _
                Brushes.Black, 480, 580, fmtString)
        e.Graphics.DrawString(txtQuantity4.Text, fntString, _
                Brushes.Black, 540, 580, fmtString)
        e.Graphics.DrawString(txtSubTotal4.Text, fntString, _
                Brushes.Black, 630, 580, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 600, 640, 600)
    
        e.Graphics.DrawString(txtPartName5.Text, fntString, _
                Brushes.Black, 100, 610)
        e.Graphics.DrawString(txtUnitPrice5.Text, fntString, _
                Brushes.Black, 480, 610, fmtString)
        e.Graphics.DrawString(txtQuantity5.Text, fntString, _
                Brushes.Black, 540, 610, fmtString)
        e.Graphics.DrawString(txtSubTotal5.Text, fntString, _
                Brushes.Black, 630, 610, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 630, 640, 630)
    
        fntString = New Font("Times New Roman", _
    			12, FontStyle.Bold)
        e.Graphics.DrawString("Jobs Performed", fntString, _
                Brushes.Black, 80, 650)
        e.Graphics.DrawLine(New Pen(Color.Black, 2), _
    			80, 670, 680, 670)
    
        e.Graphics.DrawString("Job Name", fntString, _
    			Brushes.Black, 100, 680)
        e.Graphics.DrawString("Price", fntString, _
    			Brushes.Black, 562, 680)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 700, 640, 700)
    
        fntString = New Font("Times New Roman", 10, _
    			FontStyle.Regular)
    
        e.Graphics.DrawString(txtJobDescription1.Text, fntString, _
                Brushes.Black, 100, 710)
        e.Graphics.DrawString(txtJobPrice1.Text, fntString, _
                Brushes.Black, 600, 710, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 730, 640, 730)
    
        e.Graphics.DrawString(txtJobDescription2.Text, fntString, _
                Brushes.Black, 100, 740)
        e.Graphics.DrawString(txtJobPrice2.Text, fntString, _
                Brushes.Black, 600, 740, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 760, 640, 760)
    
        e.Graphics.DrawString(txtJobDescription3.Text, fntString, _
                Brushes.Black, 100, 770)
        e.Graphics.DrawString(txtJobPrice3.Text, fntString, _
                    Brushes.Black, 600, 770, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 790, 640, 790)
    
        e.Graphics.DrawString(txtJobDescription4.Text, _
    			fntString, Brushes.Black, 100, 800)
        e.Graphics.DrawString(txtJobPrice4.Text, fntString, _
                    Brushes.Black, 600, 800, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 820, 640, 820)
    
        e.Graphics.DrawString(txtJobDescription5.Text, _
    			fntString, Brushes.Black, 100, 830)
        e.Graphics.DrawString(txtJobPrice5.Text, fntString, _
                    Brushes.Black, 600, 830, fmtString)
        e.Graphics.DrawLine(New Pen(Color.Black, 1), _
    			100, 850, 640, 850)
    
        fntString = New System.Drawing.Font("Times New Roman", _
      			12, FontStyle.Bold)
        e.Graphics.DrawString("Order Summary", fntString, _
                    Brushes.Black, 80, 870)
        e.Graphics.DrawLine(New Pen(Color.Black, 2), _
    			80, 890, 680, 890)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Bold)
        e.Graphics.DrawString("Total Parts:", fntString, _
                    Brushes.Black, 500, 900)
        fntString = New System.Drawing.Font("Times New Roman", _
     			10, FontStyle.Regular)
        e.Graphics.DrawString(txtTotalParts.Text, fntString, _
                    Brushes.Black, 640, 900, fmtString)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Bold)
        e.Graphics.DrawString("Total Labor:", fntString, _
                    Brushes.Black, 500, 920)
        fntString = New System.Drawing.Font("Times New Roman", _
     			10, FontStyle.Regular)
        e.Graphics.DrawString(txtTotalLabor.Text, fntString, _
                    Brushes.Black, 640, 920, fmtString)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Bold)
        e.Graphics.DrawString("Tax Rate:", fntString, _
                    Brushes.Black, 500, 940)
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Regular)
        e.Graphics.DrawString(txtTaxRate.Text, fntString, _
                    Brushes.Black, 640, 940, fmtString)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Bold)
        e.Graphics.DrawString("Tax Amount:", fntString, _
                    Brushes.Black, 500, 960)
        fntString = New System.Drawing.Font("Times New Roman", _
     			10, FontStyle.Regular)
        e.Graphics.DrawString(txtTaxAmount.Text, fntString, _
                    Brushes.Black, 640, 960, fmtString)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Bold)
        e.Graphics.DrawString("Repair Total:", fntString, _
                    Brushes.Black, 500, 980)
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Regular)
        e.Graphics.DrawString(txtTotalOrder.Text, fntString, _
                    Brushes.Black, 640, 980, fmtString)
    
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Bold)
        e.Graphics.DrawString("Recommendations:", fntString, _
                Brushes.Black, 100, 900)
        fntString = New System.Drawing.Font("Times New Roman", _
    			10, FontStyle.Regular)
        e.Graphics.DrawString(txtRecommendations.Text, fntString, _
                    Brushes.Black, New RectangleF(100, 920, 350, 280))
    End Sub
  21. Return to the form
  22. From the Printing section of the Toolbox, click the PrintDialog button PrintDialog and click the form
  23. While the print control is still selected, in the Properties window, change its Name to dlgPrint 
  24. Click Document and select docPrint
  25. Right-click the form and click View Code
  26. In the Class Name combo box, select mnuFilePrint
  27. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuFilePrint_Click(ByVal sender As Object, _
                                       ByVal e As System.EventArgs) _
                                       Handles mnuFilePrint.Click
            If dlgPrint.ShowDialog() = DialogResult.OK Then
                docPrint.Print()
            End If
    End Sub
  28. Return to the form
  29. From the Printing section of the Toolbox, click PrintPreviewDialog and click the form
  30. In the Properties window, change its (Name) to dlgPrintPreview
  31. Still in the Properties windows, set its Document property to docPrint
  32. Right-click the form and click View Code
  33. In the Class Name combo box, select mnuFilePrintPreview
  34. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuFilePrintPreview_Click(ByVal sender As Object, _
                                              ByVal e As System.EventArgs) _
                                              Handles mnuFilePrintPreview.Click
            dlgPrintPreview.ShowDialog()
    End Sub
  35. Save the file
  36. Return to the form
  37. From the Dialogs section of the Toolbox, click SaveFileDialog and click the form
  38. Change its properties as follows:
    Title: Save Current Repair Order
    DefaultExt: xml
    Filter: Repair Orders (*.xml)|*.xml|All Files|
    Name: dlgSave
  39. Right-click the form and click View Code
  40. In the Class Name combo box, select mnuFileSave
  41. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuFileSave_Click(ByVal sender As Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles mnuFileSave.Click
        ' Just in case, calculate the order now
        Calculate()
        ' This number will be used to incrementally
        ' create the files by their names
        Dim iFilename As Integer
        Dim Filename As String
        ' If this directory doesn't exist, create it
        ' Also, get a reference to this directory for later use
        Dim FolderName As String = "C:\College Park Auto Repair"
        Dim Folder As DirectoryInfo = Directory.CreateDirectory(FolderName)
    
        ' Get the list of files, if any, from the above folder
        Dim ListOfFiles() As FileInfo = Folder.GetFiles("*.xml")
    
        ' If there is no XML file in the directory,
        ' then get ready to create the first file
        If ListOfFiles.Length = 0 Then
            ' Create the name of the (initial) file
            iFilename = 1000
        Else ' If there was at least one file in the directory
            ' Get a reference to the last file
            Dim fleLast As FileInfo = ListOfFiles(ListOfFiles.Length - 1)
            ' Get the name of the last file without its extension
            Dim fwe As String = Path.GetFileNameWithoutExtension(fleLast.FullName)
            ' Get the name of the file
            iFilename = CInt(fwe)
        End If
    
        Filename = FolderName& "\" & cstr(iFilename + 1)& ".xml"
    
        ' Get ready to display it in the Save dialog box
        dlgSave.FileName = Filename
    
        ' Find out if the user clicked OK after displaying the Save dialog box
        If dlgSave.ShowDialog() = DialogResult.OK Then
            Dim wtrRepairOrder As XmlTextWriter = New XmlTextWriter(Filename, _
                        Encoding.UTF8)
    
            ' Create the contents of the XML file
            ' Notice that we are not making an attempt to check the values
            wtrRepairOrder.WriteStartDocument()
    
            wtrRepairOrder.WriteStartElement("RepairOrders")
            wtrRepairOrder.WriteStartElement("Invoice")
    
            wtrRepairOrder.WriteElementString("CustomerName", txtCustomerName.Text)
                wtrRepairOrder.WriteElementString("Addres", txtAddress.Text)
                wtrRepairOrder.WriteElementString("City", txtCity.Text)
                wtrRepairOrder.WriteElementString("State", txtState.Text)
                wtrRepairOrder.WriteElementString("ZIPCode", txtZIPCode.Text)
                wtrRepairOrder.WriteElementString("Make", txtMake.Text)
                wtrRepairOrder.WriteElementString("Model", txtModel.Text)
                wtrRepairOrder.WriteElementString("CarYear", txtCarYear.Text)
            wtrRepairOrder.WriteElementString("ProbDescription", txtProblem.Text)
                wtrRepairOrder.WriteElementString("PartName1", txtPartName1.Text)
                wtrRepairOrder.WriteElementString("UnitPrice1", txtUnitPrice1.Text)
                wtrRepairOrder.WriteElementString("Quantity1", txtQuantity1.Text)
                wtrRepairOrder.WriteElementString("SubTotal1", txtSubTotal1.Text)
                wtrRepairOrder.WriteElementString("PartName2", txtPartName2.Text)
                wtrRepairOrder.WriteElementString("UnitPrice2", txtUnitPrice2.Text)
                wtrRepairOrder.WriteElementString("Quantity2", txtQuantity2.Text)
                wtrRepairOrder.WriteElementString("SubTotal2", txtSubTotal2.Text)
                wtrRepairOrder.WriteElementString("PartName3", txtPartName3.Text)
                wtrRepairOrder.WriteElementString("UnitPrice3", txtUnitPrice3.Text)
                wtrRepairOrder.WriteElementString("Quantity3", txtQuantity3.Text)
                wtrRepairOrder.WriteElementString("SubTotal3", txtSubTotal3.Text)
                wtrRepairOrder.WriteElementString("PartName4", txtPartName4.Text)
                wtrRepairOrder.WriteElementString("UnitPrice4", txtUnitPrice4.Text)
                wtrRepairOrder.WriteElementString("Quantity4", txtQuantity4.Text)
                wtrRepairOrder.WriteElementString("SubTotal4", txtSubTotal4.Text)
                wtrRepairOrder.WriteElementString("PartName5", txtPartName5.Text)
                wtrRepairOrder.WriteElementString("UnitPrice5", txtUnitPrice5.Text)
                wtrRepairOrder.WriteElementString("Quantity5", txtQuantity5.Text)
                wtrRepairOrder.WriteElementString("SubTotal5", txtSubTotal5.Text)
            wtrRepairOrder.WriteElementString("JobDescription1", _
    					  txtJobDescription1.Text)
                wtrRepairOrder.WriteElementString("JobPrice1", txtJobPrice1.Text)
            wtrRepairOrder.WriteElementString("JobDescription2", _
    					  txtJobDescription2.Text)
                wtrRepairOrder.WriteElementString("JobPrice2", txtJobPrice2.Text)
            wtrRepairOrder.WriteElementString("JobDescription3", _
    					  txtJobDescription3.Text)
                wtrRepairOrder.WriteElementString("JobPrice3", txtJobPrice3.Text)
            wtrRepairOrder.WriteElementString("JobDescription4", _
    					  txtJobDescription4.Text)
                wtrRepairOrder.WriteElementString("JobPrice4", txtJobPrice4.Text)
            wtrRepairOrder.WriteElementString("JobDescription5", _
    					  txtJobDescription5.Text)
                wtrRepairOrder.WriteElementString("JobPrice5", txtJobPrice5.Text)
                wtrRepairOrder.WriteElementString("TotalPart", txtTotalParts.Text)
                wtrRepairOrder.WriteElementString("TotalLabor", txtTotalLabor.Text)
                wtrRepairOrder.WriteElementString("TaxRate", txtTaxRate.Text)
                wtrRepairOrder.WriteElementString("TaxAmount", txtTaxAmount.Text)
                wtrRepairOrder.WriteElementString("TotalOrder", txtTotalOrder.Text)
            wtrRepairOrder.WriteElementString("Recommendation", _
    					  txtRecommendations.Text)
    
                wtrRepairOrder.WriteEndElement()
                wtrRepairOrder.WriteEndElement()
    
                wtrRepairOrder.WriteEndDocument()
    
                wtrRepairOrder.Flush()
                wtrRepairOrder.Close()
            End If
    End Sub
  42. Execute the application to test it
  43. Create a new record and click Calculate Order:
     
    College Park Auto Repair
  44. Click File -> Save and OK to Save the file
  45. Click File -> New Repair Order
  46. Create another repair order:
     
  47. Save it
  48. Close the form and return to your programming environment
  49. Display the form.
    From the Dialogs section of the Toolbox, click OpenFileDialog and click the form
  50. Change its properties as follows:
    Title: Open Existing Repair Order
    DefaultExt: xml
    Filter: Repair Orders (*.xml)|*.xml|All Files|
    Name: dlgOpen
  51. Right-click the form and click View Code
  52. In the Class Name combo box, select mnuFileOpen
  53. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuFileOpen_Click(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles mnuFileOpen.Click
        Dim rdrRepairOrder As XmlTextReader = Nothing
    
        Try
            If dlgOpen.ShowDialog() = DialogResult.OK Then
                rdrRepairOrder = New XmlTextReader(dlgOpen.FileName)
    
                ' Scan the XML file
                Do
                    ' every time you find an element, find out what type it is
                    ' If you find text, put it in the combo box' list
                    If (XmlNodeType.Element <> 0) And _
                       (rdrRepairOrder.Name = "CustomerName") Then
                        txtCustomerName.Text = _
                        rdrRepairOrder.ReadElementString("CustomerName")
                        txtAddress.Text = _
    			rdrRepairOrder.ReadElementString("Addres")
                        txtCity.Text = rdrRepairOrder.ReadElementString("City")
                        txtState.Text = rdrRepairOrder.ReadElementString("State")
                        txtZIPCode.Text = _
    			rdrRepairOrder.ReadElementString("ZIPCode")
                        txtMake.Text = rdrRepairOrder.ReadElementString("Make")
                        txtModel.Text = rdrRepairOrder.ReadElementString("Model")
                        txtCarYear.Text = _
    			rdrRepairOrder.ReadElementString("CarYear")
                        txtProblem.Text = _
                        rdrRepairOrder.ReadElementString("ProbDescription")
                        txtPartName1.Text = _
    			rdrRepairOrder.ReadElementString("PartName1")
                        txtUnitPrice1.Text = _
    			rdrRepairOrder.ReadElementString("UnitPrice1")
                        txtQuantity1.Text = _
    			rdrRepairOrder.ReadElementString("Quantity1")
                        txtSubTotal1.Text = _
    			rdrRepairOrder.ReadElementString("SubTotal1")
                        txtPartName2.Text = _
    			rdrRepairOrder.ReadElementString("PartName2")
                        txtUnitPrice2.Text = _
    			rdrRepairOrder.ReadElementString("UnitPrice2")
                        txtQuantity2.Text = _
    			rdrRepairOrder.ReadElementString("Quantity2")
                        txtSubTotal2.Text = _
    			rdrRepairOrder.ReadElementString("SubTotal2")
                        txtPartName3.Text = _
    			rdrRepairOrder.ReadElementString("PartName3")
                        txtUnitPrice3.Text = _
    			rdrRepairOrder.ReadElementString("UnitPrice3")
                        txtQuantity3.Text = _
    			rdrRepairOrder.ReadElementString("Quantity3")
                        txtSubTotal3.Text = _
    			rdrRepairOrder.ReadElementString("SubTotal3")
                        txtPartName4.Text = _
    			rdrRepairOrder.ReadElementString("PartName4")
                        txtUnitPrice4.Text = _
    			rdrRepairOrder.ReadElementString("UnitPrice4")
                        txtQuantity4.Text = _
    			rdrRepairOrder.ReadElementString("Quantity4")
                        txtSubTotal4.Text = _
    			rdrRepairOrder.ReadElementString("SubTotal4")
                        txtPartName5.Text = _
    			rdrRepairOrder.ReadElementString("PartName5")
                        txtUnitPrice5.Text = _
    			rdrRepairOrder.ReadElementString("UnitPrice5")
                        txtQuantity5.Text = _
    			rdrRepairOrder.ReadElementString("Quantity5")
                        txtSubTotal5.Text = _
    			rdrRepairOrder.ReadElementString("SubTotal5")
                        txtJobDescription1.Text = _
                        rdrRepairOrder.ReadElementString("JobDescription1")
                        txtJobPrice1.Text = _
    			rdrRepairOrder.ReadElementString("JobPrice1")
                        txtJobDescription2.Text = _
                        rdrRepairOrder.ReadElementString("JobDescription2")
                        txtJobPrice2.Text = _
    			rdrRepairOrder.ReadElementString("JobPrice2")
                        txtJobDescription3.Text = _
                        rdrRepairOrder.ReadElementString("JobDescription3")
                        txtJobPrice3.Text = _
    			rdrRepairOrder.ReadElementString("JobPrice3")
                        txtJobDescription4.Text = _
                        rdrRepairOrder.ReadElementString("JobDescription4")
                        txtJobPrice4.Text = _
    			rdrRepairOrder.ReadElementString("JobPrice4")
                        txtJobDescription5.Text = _
                        rdrRepairOrder.ReadElementString("JobDescription5")
                        txtJobPrice5.Text = _
    			rdrRepairOrder.ReadElementString("JobPrice5")
                        txtTotalParts.Text = _
    			rdrRepairOrder.ReadElementString("TotalPart")
                        txtTotalLabor.Text = _
    			rdrRepairOrder.ReadElementString("TotalLabor")
                        txtTaxRate.Text = _
    			rdrRepairOrder.ReadElementString("TaxRate")
                        txtTaxAmount.Text = _
    			rdrRepairOrder.ReadElementString("TaxAmount")
                        txtTotalOrder.Text = _
    			rdrRepairOrder.ReadElementString("TotalOrder")
                        txtRecommendations.Text = _
                        rdrRepairOrder.ReadElementString("Recommendation")
                    End If
                Loop While rdrRepairOrder.Read()
            End If
        Catch Exc As XmlException
            MsgBox("The file name you provided is not valid")
        Finally
            rdrRepairOrder.Close()
        End Try
    End Sub
  54. Execute the application and open one the previous orders
  55. In the Class Name combo box, select mnuFileSave and, to control the indentation when the file is saved, change the top section of the event as follows:
     
    Private Sub mnuFileSave_Click(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles mnuFileSave.Click
        . . . No Change
    
        ' Find out if the user clicked OK after displaying the Save dialog box
        If dlgSave.ShowDialog() = DialogResult.OK Then
            Dim wtrRepairOrder As XmlTextWriter = New XmlTextWriter(Filename, _
                        Encoding.UTF8)
            wtrRepairOrder.Formatting = Formatting.Indented
            wtrRepairOrder.Indentation = 4
    
            . . . No Change
        End If
    End Sub
  56. In the Class Name combo box, select mnuFileExit
  57. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuFileExit_Click(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles mnuFileExit.Click
        End
    End Sub
  58. Execute the application
  59. Create and save a few more repair orders
  60. Close the form

Exercises

 

College Park Auto-Repair

  1. Open the College Park Auto-Repair database from this lesson
  2. Notice that if you open a repair order, change it and try to save it, the application would create a new repair order with a new receipt number.
    Configure the form so that, if a repair order was previous opened then changed, if the user saves it, the current repair order would be saved, using its own receipt number instead of generating a new receipt number
 

Home Copyright © 2008-2012 FunctionX