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:
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.
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:
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.
The PrintPreviewDialog component provides access to several encapsulated features, including the following:
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.
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
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.