Export (0) Print
Expand All
1 out of 5 rated this helpful - Rate this topic

Displaying Print Preview in Windows Applications

Visual Studio .NET 2003

The PrintPreviewDialog control is used to display a document, often before it is to be printed.

You need to specify an instance of the PrintDocument class; this is the document to be printed. For more information on printing with the PrintDocument component, see Creating Standard Windows Forms Print Jobs.

Note   To use the PrintPreviewDialog control at run time, users must have a printer installed on their computer, either locally or through a network, as this is partly how the PrintPreviewDialog component determines how a document will look when printed.

The PrintPreviewDialog control uses the PrinterSettings class. Additionally, the PrintPreviewDialog control uses the PageSettings class, just as the PageSetupDialog component does. The print document specified in the PrintPreviewDialog control's Document property refers to instances of both the PrinterSettings and PageSettings classes, and these are used to render the document in the preview window.

To view pages using the PrintPreviewDialog control

  • Use the ShowDialog method to display the dialog box, specifying the PrintDocument to use.

    In the example below, the Button control's Click event handler opens an instance of the PrintPreviewDialog control. The print document is specified in the Document property. Note that in the example below, no print document is specified.

    The example assumes your form has a Button control, a PrintDocument component named myDocument, and a PrintPreviewDialog control.

    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
       ' The print document 'myDocument' used below
       ' is merely for an example.
       ' You will have to specify your own print document.
       PrintPreviewDialog1.Document = myDocument
       PrintPreviewDialog1.ShowDialog()
    End Sub
    
    // C#
    private void button1_Click(object sender, System.EventArgs e)
    {
       // The print document 'myDocument' used below
       // is merely for an example.
       // You will have to specify your own print document.
       printPreviewDialog1.Document = myDocument;
       printPreviewDialog1.ShowDialog();
    }
    
    // C++
    private:
       System::Void button1_Click(System::Object *  sender,
          System::EventArgs *  e)
       {
          // The print document 'myDocument' used below
          // is merely for an example.
          // You will have to specify your own print document.
          printPreviewDialog1->Document = myDocument;
          printPreviewDialog1->ShowDialog();
       }
    
    Visual C# and Visual C++ Note   Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    // C#
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // C++
    this->button1->Click += new
    System::EventHandler(this, button1_Click);

See Also

PrintDocument Component | PrintPreviewDialog Control | Windows Forms Print Support | Windows Forms

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.