File-Based Applications:
College Park Auto-Shop



The .NET Framework has a great deal of support for file processing. It does this through various stream-based classes of the System.IO namespace. For example, the FileInfo class provides information and operations on files. Serialization can be performed using any list-based class. Besides file processing, the .NET Framework also supports printing, which is done through the Graphics class.

Printing in Microsoft Windows has traditionally been difficult. To do this, you had to involve drawing, hooks, and callback functions, etc. In the .NET Framework, a great deal of the job was simplified by creating simple-to-use classes. In fact, many aspects were also made clearer such as what class does what and when. We reviewed those classes in Print or Page Setup. We will use the Print and Graphics classes here to see how to print.


The Print Dialog Box



Besides saving or opening files, another operation users perform on a document consists of printing it. Printing is the ability to render, on paper, the result of a control's content or the contents of various controls. This is performed using an external device called a printer peripheral or simply a printer. To do this, users need access to a printer device. There are two main ways users print a document. They can ask the application they are using to send the document directly to a printer or they can use a dialog box to decide how the printing should be done.


Practical Learning Practical Learning: Introducing Printing

  1. Start Microsoft Visual Basic or Visual Studio .NET and create a new Windows Forms application named CPAS1
  2. Design the form as follows:
    Control Name Text Other Properties
    Group   Order Identification  
    Label   Order Date:  
    DateTimePicker dtpOrderDate    
    Label   Order Time:  
    DateTimePicker dtpOrderTime   Format: Time
    Label   Order Number:  
    TextBox txtOrderNumber    
    GroupBox   Customer and Car Information  
    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   Recommendations to Customer  
    TextBox txtRecommendations   Multiline: True
    ScrollBars: Vertical
    GroupBox   Order Summary  
    Button btnCalculateOrder Calculate Order  
    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   Repair Total:  
    TextBox txtRepairTotal 0.00 TextAlign: Right
    Button btnSave Save this Order in the Above Date and Start New Order  
    Button btnOpen Open a Previous Repair Order  
    Button btnResetOrder Reset Order  
    Button btnPrint Print Repair Order  
    Button btnPreview Print Preview  
    Button btnClose Close  
  3. Save all
  4. On the form, double-click the Calculate Order button and implement its Click event as follows:
    Private Sub btnCalculateOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                    Handles btnCalculateOrder.Click
            Dim part1UnitPrice As Double
            Dim part1SubTotal As Double
            Dim part2UnitPrice As Double
            Dim part2SubTotal As Double
            Dim part3UnitPrice As Double
            Dim part3SubTotal As Double
            Dim part4UnitPrice As Double
            Dim part4SubTotal As Double
            Dim part5UnitPrice As Double
            Dim part5SubTotal As Double
            Dim totalParts As Double
            Dim part1Quantity As Integer
            Dim part2Quantity As Integer
            Dim part3Quantity As Integer
            Dim part4Quantity As Integer
            Dim part5Quantity As Integer
            Dim job1Price As Double
            Dim job2Price As Double
            Dim job3Price As Double
            Dim job4Price As Double
            Dim job5Price As Double
            Dim totalLabor As Double
            Dim taxRate As Double
            Dim taxAmount As Double
            Dim totalOrder As Double
            ' Don't charge a part unless it is clearly identified
            If Me.txtPartName1.Text = "" Then
                Me.txtUnitPrice1.Text = "0.00"
                Me.txtQuantity1.Text = "0"
                Me.txtSubTotal1.Text = "0.00"
                part1UnitPrice = 0.0
                    part1UnitPrice = CDbl(Me.txtUnitPrice1.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Unit Price")
                    Me.txtUnitPrice1.Text = "0.00"
                End Try
                    part1Quantity = CInt(Me.txtQuantity1.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Quantity")
                    Me.txtQuantity1.Text = "0"
                End Try
            End If
            If Me.txtPartName2.Text = "" Then
                Me.txtUnitPrice2.Text = "0.00"
                Me.txtQuantity2.Text = "0"
                Me.txtSubTotal2.Text = "0.00"
                part2UnitPrice = 0.0
                    part2UnitPrice = CDbl(Me.txtUnitPrice2.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Unit Price")
                    Me.txtUnitPrice2.Text = "0.00"
                End Try
                    part2Quantity = CInt(Me.txtQuantity2.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Quantity")
                    Me.txtQuantity2.Text = "0"
                End Try
            End If
            If Me.txtPartName3.Text = "" Then
                Me.txtUnitPrice3.Text = "0.00"
                Me.txtQuantity3.Text = "0"
                Me.txtSubTotal3.Text = "0.00"
                part3UnitPrice = 0.0
                    part3UnitPrice = CDbl(Me.txtUnitPrice3.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Unit Price")
                    Me.txtUnitPrice3.Text = "0.00"
                End Try
                    part3Quantity = CInt(Me.txtQuantity3.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Quantity")
                    Me.txtQuantity3.Text = "0"
                End Try
            End If
            If Me.txtPartName4.Text = "" Then
                Me.txtUnitPrice4.Text = "0.00"
                Me.txtQuantity4.Text = "0"
                Me.txtSubTotal4.Text = "0.00"
                part4UnitPrice = 0.0
                    part4UnitPrice = CDbl(Me.txtUnitPrice4.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Unit Price")
                    Me.txtUnitPrice4.Text = "0.00"
                End Try
                    part4Quantity = CInt(Me.txtQuantity4.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Quantity")
                    Me.txtQuantity4.Text = "0"
                End Try
            End If
            If Me.txtPartName5.Text = "" Then
                Me.txtUnitPrice5.Text = "0.00"
                Me.txtQuantity5.Text = "0"
                Me.txtSubTotal5.Text = "0.00"
                part5UnitPrice = 0.0
                    part5UnitPrice = CDbl(Me.txtUnitPrice5.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Unit Price")
                    Me.txtUnitPrice5.Text = "0.00"
                End Try
                    part5Quantity = CInt(Me.txtQuantity5.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Quantity")
                    Me.txtQuantity5.Text = "0"
                End Try
            End If
            ' Don't bill the customer for a job that is not specified
            If Me.txtJobPerformed1.Text = "" Then
                Me.txtJobPrice1.Text = "0.00"
                job1Price = 0.0
                    job1Price = CDbl(Me.txtJobPrice1.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Job Price")
                    Me.txtJobPrice1.Text = "0.00"
                End Try
            End If
            If Me.txtJobPerformed2.Text = "" Then
                Me.txtJobPrice2.Text = "0.00"
                job2Price = 0.0
                    job2Price = CDbl(Me.txtJobPrice2.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Job Price")
                    Me.txtJobPrice2.Text = "0.00"
                End Try
            End If
            If Me.txtJobPerformed3.Text = "" Then
                Me.txtJobPrice3.Text = "0.00"
                job3Price = 0.0
                    job3Price = CDbl(Me.txtJobPrice3.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Job Price")
                    Me.txtJobPrice3.Text = "0.00"
                End Try
            End If
            If Me.txtJobPerformed4.Text = "" Then
                Me.txtJobPrice4.Text = "0.00"
                job4Price = 0.0
                    job4Price = CDbl(Me.txtJobPrice4.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Job Price")
                    Me.txtJobPrice4.Text = "0.00"
                End Try
            End If
            If Me.txtJobPerformed5.Text = "" Then
                Me.txtJobPrice5.Text = "0.00"
                job5Price = 0.0
                    job5Price = CDbl(Me.txtJobPrice5.Text)
                Catch exc As FormatException
                    MessageBox.Show("Invalid Job Price")
                    Me.txtJobPrice5.Text = "0.00"
                End Try
            End If
            part1SubTotal = part1UnitPrice * part1Quantity
            part2SubTotal = part2UnitPrice * part2Quantity
            part3SubTotal = part3UnitPrice * part3Quantity
            part4SubTotal = part4UnitPrice * part4Quantity
            part5SubTotal = part5UnitPrice * part5Quantity
            Me.txtSubTotal1.Text = part1SubTotal.ToString("F")
            Me.txtSubTotal2.Text = part2SubTotal.ToString("F")
            Me.txtSubTotal3.Text = part3SubTotal.ToString("F")
            Me.txtSubTotal4.Text = part4SubTotal.ToString("F")
            Me.txtSubTotal5.Text = part5SubTotal.ToString("F")
            totalParts = part1SubTotal + part2SubTotal + part3SubTotal + _
                         part4SubTotal + part5SubTotal
            totalLabor = job1Price + job2Price + job3Price + _
                         job4Price + job5Price
                taxRate = CDbl(Me.txtTaxRate.Text)
            Catch exc As FormatException
                MessageBox.Show("Invalid Tax Rate")
                Me.txtTaxRate.Text = "7.75"
            End Try
            Dim totalPartsAndLabor As Double = totalParts + totalLabor
            taxAmount = totalPartsAndLabor * taxRate / 100
            totalOrder = totalPartsAndLabor + taxAmount
            Me.txtTotalParts.Text = totalParts.ToString("F")
            Me.txtTotalLabor.Text = totalLabor.ToString("F")
            Me.txtTaxAmount.Text = taxAmount.ToString("F")
            Me.txtRepairTotal.Text = totalOrder.ToString("F")
    End Sub
  5. In the Class Name combo box, select btnReset
  6. In the Method name combo box, select Click and implement the event as follows:
    Private Sub btnResetOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    				Handles btnResetOrder.Click
            Me.txtCustomerName.Text = ""
            Me.txtAddress.Text = ""
            Me.txtCity.Text = ""
            Me.txtState.Text = ""
            Me.txtZIPCode.Text = ""
            Me.txtMake.Text = ""
            Me.txtModel.Text = ""
            Me.txtCarYear.Text = ""
            Me.txtProblem.Text = ""
            Me.txtPartName1.Text = ""
            Me.txtUnitPrice1.Text = "0.00"
            Me.txtQuantity1.Text = "0"
            Me.txtSubTotal1.Text = "0.00"
            Me.txtPartName2.Text = ""
            Me.txtUnitPrice2.Text = "0.00"
            Me.txtQuantity2.Text = "0"
            Me.txtSubTotal2.Text = "0.00"
            Me.txtPartName3.Text = ""
            Me.txtUnitPrice3.Text = "0.00"
            Me.txtQuantity3.Text = "0"
            Me.txtSubTotal3.Text = "0.00"
            Me.txtPartName4.Text = ""
            Me.txtUnitPrice4.Text = "0.00"
            Me.txtQuantity4.Text = "0"
            Me.txtSubTotal4.Text = "0.00"
            Me.txtPartName5.Text = ""
            Me.txtUnitPrice5.Text = "0.00"
            Me.txtQuantity5.Text = "0"
            Me.txtSubTotal5.Text = "0.00"
            Me.txtJobPerformed1.Text = ""
            Me.txtJobPrice1.Text = "0.00"
            Me.txtJobPerformed2.Text = ""
            Me.txtJobPrice2.Text = "0.00"
            Me.txtJobPerformed3.Text = ""
            Me.txtJobPrice3.Text = "0.00"
            Me.txtJobPerformed4.Text = ""
            Me.txtJobPrice4.Text = "0.00"
            Me.txtJobPerformed5.Text = ""
            Me.txtJobPrice5.Text = "0.00"
            Me.txtTotalParts.Text = "0.00"
            Me.txtTotalLabor.Text = "0.00"
            Me.txtTaxRate.Text = "7.75"
            Me.txtTaxAmount.Text = "0.00"
            Me.txtRepairTotal.Text = "0.00"
            Me.txtRecommendations.Text = ""
    End Sub
  7. Return to the form. In the Windows Forms section of the Toolbox, click SaveFileDialog and click the form
  8. Change its properties as follows:
    DefaultExt: rpr
    Filter: Customer Repairs (*.rpr)|*.rpr|All Files|
    Title: Save Current Repair Order
  9. In the Windows Forms section of the Toolbox, click OpenFileDialog and click the form
  10. Change its properties as follows:
    DefaultExt: rpr
    Filter: Customer Repairs (*.rpr)|*.rpr|All Files|
    Title: Open Existing Repair Order
  11. Right-click the form and click View Code
  12. In the top section of the file, type the following:
    Imports System.IO
    Imports System.Globalization
    Public Class Form1
        Inherits System.Windows.Forms.Form
  13. In the Class Name combo box, select btnSave
  14. In the Method Name combo box, select Click and implement the event as follows:
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                                    Handles btnSave.Click
            ' Just in case the user forgot to first calculate, do it now
            Me.btnCalculateOrder_Click(sender, e)
            ' Get the date this order is/was processed
            Dim selDate As DateTime = Me.dtpOrderDate.Value
            ' The list of months
            Dim strMonth() As String = {"January", "February", "March", "April", _
                                  "May", "June", "July", "August", _
               "September", "October", "November", "December"}
            ' Locate the numeric value of the month from the selected date
            Dim selMonth As Integer = selDate.Month - 1
            ' Locate the year value
            Dim selYear As Integer = selDate.Year
            ' This is the name of the folder where the order will be saved
            Dim strFolder As String = "C:\College Park Auto Shop\" & _
                strMonth(selMonth) & " " & CStr(selYear)
            ' This number will be used to incrementally create the files by their names
            Dim incremental As Integer = 1000
            ' Check the above folder. If it exists, don't create it
            ' If it doesn't exist, then create it
            Dim dirInfo As DirectoryInfo = Directory.CreateDirectory(strFolder)
            ' Get the list of files, if any, from the above folder
            Dim fleList() As FileInfo = dirInfo.GetFiles()
            ' If there is no file in the directory, then get ready to create the first file
            If fleList.Length = 0 Then
                ' Get ready to display it in the Save dialog box
                Me.saveFileDialog1.FileName = Me.txtOrderNumber.Text
            Else ' If there was at least one file in the directory
                ' Get a reference to the last file
                Dim fleLast As FileInfo = fleList(fleList.Length - 1)
                ' Get the name of the last file without its extension
                Dim fwe As String = Path.GetFileNameWithoutExtension(fleLast.FullName)
                ' Increment the name of the file by 1
                incremental = CInt(fwe) + 1
                ' Get ready to display it in the Save dialog box
                Me.saveFileDialog1.FileName = incremental.ToString()
            End If
            ' For convenience, display the Save dialog box in the directory
            ' created above
            Me.saveFileDialog1.InitialDirectory = dirInfo.FullName
            ' Find out if the user clicked OK after displaying the Save dialog box
            If Me.saveFileDialog1.ShowDialog() = DialogResult.OK Then
                ' Create a new file using the name of the Save dialog box
                Dim fleCPAS As FileStream = New FileStream(Me.saveFileDialog1.FileName, _
        FileMode.Create, FileAccess.Write, FileShare.Write)
                Dim bnrCPAS As BinaryWriter = New BinaryWriter(fleCPAS)
                ' Write each value in the file
                Me.btnResetOrder_Click(sender, e)
            End If
    End Sub
  15. In the Class Name combo box, select btnOpen
  16. In the Method name combo box, select Click and implement the event as follows:
    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
            Me.openFileDialog1.InitialDirectory = "C:\College Park Auto Shop"
            If Me.openFileDialog1.ShowDialog() = DialogResult.OK Then
                Dim fleCPAS As FileStream = New FileStream(Me.openFileDialog1.FileName, _
        FileMode.Open, FileAccess.Read, FileShare.Read)
                Dim bnrCPAS As BinaryReader = New BinaryReader(fleCPAS)
                Me.dtpOrderDate.Value = _
              DateTime.Parse(bnrCPAS.ReadString(), New System.Globalization.CultureInfo("en-US", True))
                Me.dtpOrderTime.Value = _
              DateTime.Parse(bnrCPAS.ReadString(), New System.Globalization.CultureInfo("en-US", True))
                Me.txtOrderNumber.Text = bnrCPAS.ReadString()
                Me.txtCustomerName.Text = bnrCPAS.ReadString()
                Me.txtAddress.Text = bnrCPAS.ReadString()
                Me.txtCity.Text = bnrCPAS.ReadString()
                Me.txtState.Text = bnrCPAS.ReadString()
                Me.txtZIPCode.Text = bnrCPAS.ReadString()
                Me.txtMake.Text = bnrCPAS.ReadString()
                Me.txtModel.Text = bnrCPAS.ReadString()
                Me.txtCarYear.Text = bnrCPAS.ReadString()
                Me.txtProblem.Text = bnrCPAS.ReadString()
                Me.txtPartName1.Text = bnrCPAS.ReadString()
                Me.txtUnitPrice1.Text = bnrCPAS.ReadString()
                Me.txtQuantity1.Text = bnrCPAS.ReadString()
                Me.txtSubTotal1.Text = bnrCPAS.ReadString()
                Me.txtPartName2.Text = bnrCPAS.ReadString()
                Me.txtUnitPrice2.Text = bnrCPAS.ReadString()
                Me.txtQuantity2.Text = bnrCPAS.ReadString()
                Me.txtSubTotal2.Text = bnrCPAS.ReadString()
                Me.txtPartName3.Text = bnrCPAS.ReadString()
                Me.txtUnitPrice3.Text = bnrCPAS.ReadString()
                Me.txtQuantity3.Text = bnrCPAS.ReadString()
                Me.txtSubTotal3.Text = bnrCPAS.ReadString()
                Me.txtPartName4.Text = bnrCPAS.ReadString()
                Me.txtUnitPrice4.Text = bnrCPAS.ReadString()
                Me.txtQuantity4.Text = bnrCPAS.ReadString()
                Me.txtSubTotal4.Text = bnrCPAS.ReadString()
                Me.txtPartName5.Text = bnrCPAS.ReadString()
                Me.txtUnitPrice5.Text = bnrCPAS.ReadString()
                Me.txtQuantity5.Text = bnrCPAS.ReadString()
                Me.txtSubTotal5.Text = bnrCPAS.ReadString()
                Me.txtJobPerformed1.Text = bnrCPAS.ReadString()
                Me.txtJobPrice1.Text = bnrCPAS.ReadString()
                Me.txtJobPerformed2.Text = bnrCPAS.ReadString()
                Me.txtJobPrice2.Text = bnrCPAS.ReadString()
                Me.txtJobPerformed3.Text = bnrCPAS.ReadString()
                Me.txtJobPrice3.Text = bnrCPAS.ReadString()
                Me.txtJobPerformed4.Text = bnrCPAS.ReadString()
                Me.txtJobPrice4.Text = bnrCPAS.ReadString()
                Me.txtJobPerformed5.Text = bnrCPAS.ReadString()
                Me.txtJobPrice5.Text = bnrCPAS.ReadString()
                Me.txtTotalParts.Text = bnrCPAS.ReadString()
                Me.txtTotalLabor.Text = bnrCPAS.ReadString()
                Me.txtTaxRate.Text = bnrCPAS.ReadString()
                Me.txtTaxAmount.Text = bnrCPAS.ReadString()
                Me.txtRepairTotal.Text = bnrCPAS.ReadString()
                Me.txtRecommendations.Text = bnrCPAS.ReadString()
            End If
    End Sub
  17. In the Class Name combo box, select btnClose
  18. In the Method name combo box, select Click and implement the event as follows:
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                                    Handles btnClose.Click
    End Sub
  19. Execute the application to test it
  20. Create a new record and click Calculate Order
  21. Click the Save this Order... button and accept all defaults
  22. Create another order, calculate it and save it
  23. Try opening a previously saved order
  24. Close the form and return to your programming environment


Home Copyright © 2005-2016, FunctionX Next