WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Managing Printing in Your .NET Application
Pages: 1, 2, 3, 4

Previewing the Printout

Instead of sending the output directly to the printer, it is always good to allow the user to preview the output before he sends it to the printer for printing. This can be achieved using the PrintPreviewDialog() class:



    Private Sub btnPrint_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles btnPrint.Click

        '---show preview---
        Dim dlg As New PrintPreviewDialog()
        With dlg
            .Document = printDoc
            .ShowDialog()
        End With
    End Sub

The above code previews the output in a separate window. Here, you can click on the printer icon to send the output to the printer. You can also choose to enlarge the page or view multiple pages on one single screen (see Figure 4).


Figure 4. Previewing the print output

Note that when you click on the printer icon in the Print Preview window to print the output, all the various event handlers for the PrintDocument class will be fired again. Because of this, be sure to initialize all necessary variables that are used for the printing in the BeginPrint event handler.

Printing Multiple Pages

If you have a series of pages to print, you need to do some work. In the default Form1, add the Label and Text controls as shown in Figure 5 and name the two TextBox controls as shown.


Figure 5. Adding the additional controls to Form1

Here, we want to let users print a series of pages. Users can enter the required series of pages to print and the individual pages will be printed.

In the event handler for BeginPrint, initialize the page counter variable as shown in bold below:

    Private Sub _beginPrint( _
       ByVal sender As Object, _
       ByVal e As PrintEventArgs)

        '---initialize the page counter---
        pagecounter = CInt(txtFrom.Text)

        '---initialize the fonts---
        f_title = New Font("Arial", 16, FontStyle.Bold)
        f_body = New Font("Times New Roman", 10)
    End Sub

In the PrintPage event handler, you will determine if there are any more pages to print. If there are, setting the HasMorePages property of the PrintPageEventArgs class to True will cause the PrintPage event handler to be fired one more time. Once there are no more pages left to print, set the HasMorePages property to False.

    Private Sub _printPage( _
       ByVal sender As Object, _
       ByVal e As PrintPageEventArgs)
        ...
        '---increments the page counter---
        pagecounter += 1

        '---determine if you have more pages to print---
        If pagecounter <= txtTo.Text Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub

Figure 6 shows the user selecting to print from page 2 to 5, and the corresponding page output starting from page 2. You can navigate to other pages by clicking on the up/down arrows located at the top right corner of the preview window.


Figure 6. Previewing the multiple page output

Pages: 1, 2, 3, 4

Next Pagearrow