Home

File-Based Applications:
College Park Auto-Shop

 

Printing

One of the ways users print consists of sending the document to the printer. To directly send a document to the printer, you need to make sure that the control, whose value needs to be printed, supports printing. To accommodate the users of your application, you can provide a menu item or a button they would click. An example of such a button would be . To print, the user can click this button. That is the case when using WordPad: its Standard toolbar is equipped with such a button. With this type of printing, when the user decides to print, the whole document would be printed "as is", in color if the document is colored and if the printer supports colors. If there is more than one printer, the computer would use what is known as the default printer.

If you want users to be able to configure or customize the printing process, Microsoft Windows provides a common dialog box called Print:

The Print dialog box allows a user to select a printer if more than one is available. The user can decide either to print the whole document, to print a range of pages, or to print a portion of the document that was previously selected. The user can also decide on the number of copies to print from the document, the range specified, or the selected portion. Furthermore, the user can access the particular characteristics of the selected printer and specify how the printer should perform the job. For example, if the selected printer can print in color and the document is in color but the user wants to print in black and white, he or she can specify this using the Properties button.

In order to print, the Print dialog box must be given a document to print. This means that you must first prepare a document prior to printing. To support this, the .NET Framework provides the PrintDocument class that is defined in the System.Drawing.Printing namespace. This class is represented in the Toolbox by the PrintDocument button. Based on this, to prepare a document for printing, you can either add a PrintDocument object to your project or declare a pointer to PrintDocument.

After creating the document to print through a PrintDocument object, you can associate it with a PrintDialog. To support this, the PrintDialog class is equipped with the Document property. To specify the object that would carry the printing, you can assign the PrintDocument object to the PrintDialog.Document property.

A document to print can be made of only one or many pages. Each page has a number of characteristics. The characteristics of a page are controlled by the PrintDocument.PageSettings property which itself is based on the PageSettings class. The PageSettings class is defined in the System.Drawing.Printing namespace. This class holds the dimensions of the page, the values of the margins applied on the page, the tray that would supply the paper (since some printers have many trays), the printer resolution, whether the page would be printed in color or black and white, whether the page would be printed in Portrait or Landscape orientation, etc. If you don't want to specify these characteristics, you can set the PrintDocument.PageSettings property to DefaultPageSettings.

If you know the name of the document to be printed, you can assign it to the PrintDocument.DocumentName property.

 

Practical LearningPractical Learning: Providing the Document to Print

  1. Display the form. From the Toolbox, click the PrintDialog button and click the form
  2. From the Toolbox, click the PrintDocument button and click the form
  3. On the bar under the form, click printDialog1
  4. In the Properties window, set its Document to printDocument1
  5. On the bar under the form, double-click the printDocument1 button and implement its PrintPage event as follows:
     
    Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.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-Shop"
            Dim fntString As System.Drawing.Font = New System.Drawing.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, 2), 80, 220, 680, 220)
    
            e.Graphics.DrawString("Order #:", fntString, Brushes.Black, 500, 200)
            fntString = New System.Drawing.Font("Times New Roman", 12, FontStyle.Regular)
            e.Graphics.DrawString(Me.txtOrderNumber.Text, fntString, Brushes.Black, 575, 200)
    
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Bold)
            Dim strDateTimeFormat() As String = {"D", "T"}
            e.Graphics.DrawString("Order Date: ", fntString, Brushes.Black, 100, 230)
    
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
            Dim strDate As String = Me.dtpOrderDate.Value.ToString(strDateTimeFormat(0), DateTimeFormatInfo.InvariantInfo)
            e.Graphics.DrawString(strDate, fntString, Brushes.Black, 200, 230)
    
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Bold)
            e.Graphics.DrawString("Order Time:", fntString, Brushes.Black, 500, 230)
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
            Dim strTime As String = Me.dtpOrderTime.Value.ToString(strDateTimeFormat(1), DateTimeFormatInfo.InvariantInfo)
            e.Graphics.DrawString(strTime, fntString, Brushes.Black, 580, 230)
    
            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(Me.txtCustomerName.Text, fntString, Brushes.Black, 260, 260)
    
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 280, 640, 280)
    
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Bold)
            e.Graphics.DrawString("Address:", fntString, Brushes.Black, 100, 290)
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
            e.Graphics.DrawString(Me.txtAddress.Text, fntString, Brushes.Black, 260, 290)
    
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 310, 640, 310)
    
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
            Dim strAddress As String = String.Concat(Me.txtCity.Text, ", ", Me.txtState.Text, " ", Me.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 System.Drawing.Font("Times New Roman", 10, FontStyle.Bold)
            e.Graphics.DrawString("Car:", fntString, Brushes.Black, 100, 350)
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
            Dim strCar As String = Me.txtMake.Text & ", " & Me.txtModel.Text & ", " & Me.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 System.Drawing.Font("Times New Roman", 10, FontStyle.Bold)
            e.Graphics.DrawString("Problem Description:", fntString, Brushes.Black, 100, 380)
            fntString = New System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
            e.Graphics.DrawString(Me.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 System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
            Dim fmtString As StringFormat = New StringFormat
            fmtString.Alignment = StringAlignment.Far
    
            e.Graphics.DrawString(Me.txtPartName1.Text, fntString, Brushes.Black, 100, 490)
            e.Graphics.DrawString(Me.txtUnitPrice1.Text, fntString, Brushes.Black, 480, 490, fmtString)
            e.Graphics.DrawString(Me.txtQuantity1.Text, fntString, Brushes.Black, 540, 490, fmtString)
            e.Graphics.DrawString(Me.txtSubTotal1.Text, fntString, Brushes.Black, 630, 490, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 510, 640, 510)
    
            e.Graphics.DrawString(Me.txtPartName2.Text, fntString, Brushes.Black, 100, 520)
            e.Graphics.DrawString(Me.txtUnitPrice2.Text, fntString, Brushes.Black, 480, 520, fmtString)
            e.Graphics.DrawString(Me.txtQuantity2.Text, fntString, Brushes.Black, 540, 520, fmtString)
            e.Graphics.DrawString(Me.txtSubTotal2.Text, fntString, Brushes.Black, 630, 520, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 540, 640, 540)
    
            e.Graphics.DrawString(Me.txtPartName3.Text, fntString, Brushes.Black, 100, 550)
            e.Graphics.DrawString(Me.txtUnitPrice3.Text, fntString, Brushes.Black, 480, 550, fmtString)
            e.Graphics.DrawString(Me.txtQuantity3.Text, fntString, Brushes.Black, 540, 550, fmtString)
            e.Graphics.DrawString(Me.txtSubTotal3.Text, fntString, Brushes.Black, 630, 550, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 570, 640, 570)
    
            e.Graphics.DrawString(Me.txtPartName4.Text, fntString, Brushes.Black, 100, 580)
            e.Graphics.DrawString(Me.txtUnitPrice4.Text, fntString, Brushes.Black, 480, 580, fmtString)
            e.Graphics.DrawString(Me.txtQuantity4.Text, fntString, Brushes.Black, 540, 580, fmtString)
            e.Graphics.DrawString(Me.txtSubTotal4.Text, fntString, Brushes.Black, 630, 580, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 600, 640, 600)
    
            e.Graphics.DrawString(Me.txtPartName5.Text, fntString, Brushes.Black, 100, 610)
            e.Graphics.DrawString(Me.txtUnitPrice5.Text, fntString, Brushes.Black, 480, 610, fmtString)
            e.Graphics.DrawString(Me.txtQuantity5.Text, fntString, Brushes.Black, 540, 610, fmtString)
            e.Graphics.DrawString(Me.txtSubTotal5.Text, fntString, Brushes.Black, 630, 610, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 630, 640, 630)
    
            fntString = New System.Drawing.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 System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
    
            e.Graphics.DrawString(Me.txtJobPerformed1.Text, fntString, Brushes.Black, 100, 710)
            e.Graphics.DrawString(Me.txtJobPrice1.Text, fntString, Brushes.Black, 600, 710, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 730, 640, 730)
    
            e.Graphics.DrawString(Me.txtJobPerformed2.Text, fntString, Brushes.Black, 100, 740)
            e.Graphics.DrawString(Me.txtJobPrice2.Text, fntString, Brushes.Black, 600, 740, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 760, 640, 760)
    
            e.Graphics.DrawString(Me.txtJobPerformed3.Text, fntString, Brushes.Black, 100, 770)
            e.Graphics.DrawString(Me.txtJobPrice3.Text, fntString, Brushes.Black, 600, 770, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 790, 640, 790)
    
            e.Graphics.DrawString(Me.txtJobPerformed4.Text, fntString, Brushes.Black, 100, 800)
            e.Graphics.DrawString(Me.txtJobPrice4.Text, fntString, Brushes.Black, 600, 800, fmtString)
            e.Graphics.DrawLine(New Pen(Color.Black, 1), 100, 820, 640, 820)
    
            e.Graphics.DrawString(Me.txtJobPerformed5.Text, fntString, Brushes.Black, 100, 830)
            e.Graphics.DrawString(Me.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(Me.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(Me.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(Me.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(Me.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(Me.txtRepairTotal.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(Me.txtRecommendations.Text, fntString, _
      				Brushes.Black, New RectangleF(100, 920, 350, 280))
    End Sub
  6. In the Class Name combo box, select btnPrint
  7. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            If Me.printDialog1.ShowDialog() = DialogResult.OK Then
                Me.printDocument1.Print()
            End If
    End Sub
  8. Execute the application and create an order then click File -> Print
  9. After printing the order, save it
  10. After using it, close the form and return to your programming environment
  11. On the Toolbox, click PrintPreviewDialog and click the form
  12. In the Properties window, set its Document to printDocument1
  13. On the form, double-click the Print Preview button
  14. Implement its event as follows:
     
    Private Sub btnPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPreview.Click
            Me.printPreviewDialog1.ShowDialog()
    End Sub
  15. Execute the application
  16. Open one of the previously saved orders and click the Print Preview button
     
  17. Click Close and close the form to return to your programming environment

 
 

Previous Copyright © 2005-2016, FunctionX