Using the Printing Controls

Although it is possible to design forms that can be used to set all the PageSettings and PrinterSettings property values, it is easier to use the provided Windows components , which include the following:

  • PageSetupDialog

  • PrintPreviewDialog

  • PrintPreviewControl

  • PrintDialog

Each of these components is discussed in the following sections.

The PageSetupDialog Component

The PageSetupDialog component is the standard Windows Page Setup dialog box used in most Windows applications, as shown in Figure 11.2.

Figure 11.2. The Page Setup dialog box showing a landscape-oriented page.

graphics/11fig02.jpg

You may experiment with the use of this component by modifying the first form created in this chapter to include a Button control named btnPageSetup and a PageSetupDialog component named PageSetupDialog1 and then adding the following code to the form's code module:

 Private Sub btnPageSetup_Click(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles btnPageSetup.Click     ' Create a PageSettings object and send it to the dialog     Dim pgsCustom As PageSettings = New PageSettings()     PageSetupDialog1.PageSettings = pgsCustom     If PageSetupDialog1.ShowDialog = DialogResult.OK Then         PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings     End If End Sub 

When you click the PageSetup button, an instance of the Page Setup dialog box will be displayed. This component has the following four properties, which can be used to enable or disable different portions of the dialog box:

  • AllowMargins A value of False disables margin settings.

  • AllowOrientation A value of False disables orientation (print direction) settings.

  • AllowPaper A value of False disables paper size and source settings.

  • AllowPrinter A value of False disables the printer selection button.

The PrintPreviewDialog Component

Another standard Windows component is the PrintPreviewDialog component, which allows a user to preview printed output before it is sent to the printer.

You may experiment with the use of this component by modifying the form used in the previous section to include another Button control named btnPreview and a PrintPreviewDialog component named PrintPreviewDialog1 and then adding the following code to the form's code module:

 Private Sub btnPreview_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnPreview.Click     PrintPreviewDialog1.ShowDialog() End Sub 

When you click the Preview button, an instance of the Print Preview dialog box will be displayed, as shown in Figure 11.3.

Figure 11.3. The PrintPreviewDialog component displaying two pages of formatted output.

graphics/11fig03.jpg

The PrintPreviewDialog component provides access to several encapsulated features, including the following:

  • Printing from the preview interface (the printer icon)

  • Adjustable Zoom settings (the magnifying glass icon)

  • Multipage display modes allowing one, two, three, four, or six pages to be displayed at once

  • The ability to scroll through all pages within the document being printed

When you preview a document with the PrintPreviewDialog component, the PrintDocument events are fired , just as if you had actually printed the document.

The PrintPreviewControl Control

It is also possible to embed a print preview control directly within your form so that a separate preview dialog interface is not required. You can include this functionality within the form we have been experimenting with by modifying the code fired by the Click event of the Preview button, as follows :

 Private Sub btnPreview_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnPreview.Click     ' Expand the form to accommodate the control     Me.Height = 425     ' Create the control     Dim PrintPreviewControl1 As PrintPreviewControl = _      New System.Windows.Forms.PrintPreviewControl()     ' Set its properties     PrintPreviewControl1.Location = New System.Drawing.Point(8, 144)     PrintPreviewControl1.Name = "PrintPreviewControl1"     PrintPreviewControl1.Size = New System.Drawing.Size(272, 248)     PrintPreviewControl1.Zoom = 0.3     PrintPreviewControl1.Document = PrintDocument1     ' And add it to the form     Me.Controls.Add(PrintPreviewControl1) End Sub 

When you click the Preview button, an instance of the print preview control will be displayed within the expanded form, as shown in Figure 11.4.

Figure 11.4. A sample form showing an included instance of the PrintPreviewControl control.

graphics/11fig04.jpg

graphics/tip_icon.gif

The PrintPreviewControl control will not function properly if you drop an instance onto a form and set its propertiesit must be instantiated at runtime.


Unlike the PrintPreviewDialog component, the PrintPreviewControl control lacks the integrated feature buttons for presentation and scrolling. However, the control includes several features that may be manipulated by your code to produce the same results, as detailed in Table 11.4.

Table 11.4. Selected Properties of the PrintPreviewControl Control

Property

Meaning

AutoZoom

True automatically changes the zoom factor when the control is resized.

Columns

The number of columns of pages to display.

Rows

The number of rows of pages to display.

StartPage

The page number of the first page to display.

UseAntiAlias

True allows antialiasing when displaying the print preview. This provides higher quality text but takes longer to render.

Zoom

The zoom factor to use.

The PrintDialog Component

The final Windows component we'll discuss is the PrintDialog component, which allows a user to control a printer and its properties.

You can experiment with the use of this component by modifying the same form to include another Button control named btnPrinter and a PrintDialog component named PrintDialog1 and then adding the following code to the form's code module:

 Private Sub btnPrinter_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnPrinter.Click     Dim psCustom As PrinterSettings     PrintDialog1.PrinterSettings = psCustom     If PrintDialog1.ShowDialog = DialogResult.OK Then         PrintDocument1.PrinterSettings = psCustom     End If End Sub 

When you click the Printer button, an instance of the PrintDialog component will be displayed, as shown in Figure 11.5.

Figure 11.5. The PrintDialog component displaying current printer settings.

graphics/11fig05.jpg



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net