RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Raise Printing to a Fine Art in Windows Forms : Page 3

Learn how to format, preview, and print output the way you want it to look using the capabilities of the .NET System.Drawing.Printing namespace and the PrintDocument class.

Previewing the Printout
So far, you've seen how to print out the name card of the current employee. But what about preview functionality? During the development stage it would be very useful to see what the printout would look like before you send it to the printer (helping to save a few trees). To include print preview functionality, add a second button to the bottom of the form and name it btnPreview (see Figure 8).

Figure 8. Coming Attractions: Add a new button to the form to enable a print preview function.
Double-click on the Print (not the Preview) button and modify the button's Click event handler as shown below:

   Private Sub btnPrint_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles btnPrint.Click, btnPreview.Click
      Dim printDoc As New PrintDocument()
      AddHandler printDoc.BeginPrint, _
         New PrintEventHandler(AddressOf Me._beginPrint)
      AddHandler printDoc.PrintPage, _
         New PrintPageEventHandler( _
         AddressOf Me._printPage)
Figure 9. Preview in Action: By clicking on the new Preview button, you can see a preview of an employee record before sending it to the printer.
If CType(sender, Button).Text = "Preview" Then '---show preview--- Dim dlg As New PrintPreviewDialog() dlg.Document = printDoc dlg.ShowDialog() Else printDoc.Print() End If End Sub
Note the addition of the btnPreview.Click event to the Handles clause, which lets you use the same subroutine that services the Print button Click event. At runtime, the code checks to see which button was clicked. If the user clicked the Preview button, the code shows a preview of the printout using the PrintPreviewDialog class. Otherwise, it sends the output to the default printer.

Figure 10. Printing Multiple Employees: Add the new controls shown in this figure to allow range printing.
Figure 9 shows what the print preview looks like. It lets you send the printout to the printer or zoom in/out on the preview.

Author's Note: When you click on the printer icon in the Print Preview window, the event handlers for the PrintDocument class fire again. Be sure to initialize all variables used for printing whenever these events fire.

Printing Multiple Pages
You can now preview the printout before actually sending the output to the printer. What if you want to print the name cards several employees at once? To do this, add a few more controls as shown in Figure 10.

To print a specific number of records, users simply need to enter a range. For example, Figure 11 shows a user who wants to print employees 2 and 3.

Figure 11. Selecting an Employee Range: Using the new controls, specify the range of records you want to print.
To support printing a specific range of records, modify the BeginPrint event handler:

   Private Sub _beginPrint( _
      ByVal sender As Object, _
      ByVal e As PrintEventArgs)
      '---initialize the fonts---
      f_employeename = New Font( _
         "Arial", 10, FontStyle.Bold)
      f_title = New Font("Arial", 8)
      f_DOB = New Font("Arial", 8, FontStyle.Italic)
      '---set to the starting position to print---
      EmployeesBindingSource.Position = _
         CInt(txtFrom.Text) - 1
   End Sub
The preceding code sets the EmployeesBindingSource control to display the first record in the specified range. You then need to modify the PrintPage event handler so that after printing a record, you navigate to the next record (until reaching the last record in the range). When you set the HasMorePages property in the PrintPageEventArgs class to True, the PrintPage event fires repeatedly until you set the property to False.

Code the PrintPage event handler as follows:

   Private Sub _printPage( _
      ByVal sender As Object, _
      ByVal e As PrintPageEventArgs)
      Dim g As Graphics = e.Graphics
      Dim sizeOfString As SizeF
Figure 12: Previewing Multiple Pages: Here's the preview of a range of documents that have been selected for printing.
If EmployeesBindingSource.Position + 1 < _ CInt(txtTo.Text) Then e.HasMorePages = True EmployeesBindingSource.MoveNext() Else e.HasMorePages = False End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub
Figure 12 shows the preview of the printout. You can preview all the pages by using the arrows on the top right corner of the Preview window.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date