Export (0) Print
Expand All

PrintPreviewDialog Constructor

Initializes a new instance of the PrintPreviewDialog class.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public PrintPreviewDialog ()
public PrintPreviewDialog ()
public function PrintPreviewDialog ()

When you create an instance of PrintPreviewDialog, the Document property is initialized to a null reference (Nothing in Visual Basic). You can change the value for this property at run time.

The following code example demonstrates the PrintPreviewDialog setting the Document and UseAntiAlias properties. The example assumes the form contains a TreeView named TreeView1 that contains TreeNode objects. The Tag property of each TreeNode object must be set to a fully qualified document name that can be accessed by the machine running the example. Set each TreeNode.Text property to a string that identifies the file specified by the TreeNode.Tag property. For example, you could set TreeNode1.Tag to "c:\myDocuments\recipe.doc" and TreeNode1.Text to "recipe.doc". The example also assumes the form contains a PrintPreviewDialog named PrintPreviewDialog1 and a button named Button1. To run this example, call the InitializePrintPreviewDialog method in the form's constructor or Load event handler.


// Declare the dialog.
internal PrintPreviewDialog PrintPreviewDialog1;

// Declare a PrintDocument object named document.
private System.Drawing.Printing.PrintDocument document =
    new System.Drawing.Printing.PrintDocument();

// Initalize the dialog.
private void InitializePrintPreviewDialog()
{

    // Create a new PrintPreviewDialog using constructor.
    this.PrintPreviewDialog1 = new PrintPreviewDialog();

    //Set the size, location, and name.
    this.PrintPreviewDialog1.ClientSize = 
        new System.Drawing.Size(400, 300);
    this.PrintPreviewDialog1.Location = 
        new System.Drawing.Point(29, 29);
    this.PrintPreviewDialog1.Name = "PrintPreviewDialog1";
    
    // Associate the event-handling method with the 
    // document's PrintPage event.
    this.document.PrintPage += 
        new System.Drawing.Printing.PrintPageEventHandler
        (document_PrintPage);

    // Set the minimum size the dialog can be resized to.
    this.PrintPreviewDialog1.MinimumSize = 
        new System.Drawing.Size(375, 250);

    // Set the UseAntiAlias property to true, which will allow the 
    // operating system to smooth fonts.
    this.PrintPreviewDialog1.UseAntiAlias = true;
}

private void Button1_Click(object sender, System.EventArgs e)
{

    if (TreeView1.SelectedNode != null)

        // Set the PrintDocument object's name to the selectedNode
        // object's  tag, which in this case contains the 
        // fully-qualified name of the document. This value will 
        // show when the dialog reports progress.
    {
        document.DocumentName = TreeView1.SelectedNode.Tag.ToString();
    }

    // Set the PrintPreviewDialog.Document property to
    // the PrintDocument object selected by the user.
    PrintPreviewDialog1.Document = document;

    // Call the ShowDialog method. This will trigger the document's
    //  PrintPage event.
    PrintPreviewDialog1.ShowDialog();
}

private void document_PrintPage(object sender, 
    System.Drawing.Printing.PrintPageEventArgs e)
{

    // Insert code to render the page here.
    // This code will be called when the PrintPreviewDialog.Show 
    // method is called.

    // The following code will render a simple
    // message on the document in the dialog.
    string text = "In document_PrintPage method.";
    System.Drawing.Font printFont = 
        new System.Drawing.Font("Arial", 35, 
        System.Drawing.FontStyle.Regular);

    e.Graphics.DrawString(text, printFont, 
        System.Drawing.Brushes.Black, 0, 0);

}

// Declare the dialog.
PrintPreviewDialog printPreviewDialog1;

// Declare a PrintDocument object named document.
private System.Drawing.Printing.PrintDocument document =
    new System.Drawing.Printing.PrintDocument();

// Initalize the dialog.
private void InitializePrintPreviewDialog()
{
    // Create a new PrintPreviewDialog using constructor.
    this.printPreviewDialog1 = new PrintPreviewDialog();
    //Set the size, location, and name.
    this.printPreviewDialog1.set_ClientSize(
        new System.Drawing.Size(400, 300));
    this.printPreviewDialog1.set_Location(new System.Drawing.Point(29, 29));
    this.printPreviewDialog1.set_Name("PrintPreviewDialog1");
    // Associate the event-handling method with the 
    // document's PrintPage event.
    this.document.add_PrintPage(
        new System.Drawing.Printing.PrintPageEventHandler(
        document_PrintPage));
    // Set the minimum size the dialog can be resized to.
    this.printPreviewDialog1.set_MinimumSize(
        new System.Drawing.Size(375, 250));
    // Set the UseAntiAlias property to true, which will allow the 
    // operating system to smooth fonts.
    this.printPreviewDialog1.set_UseAntiAlias(true);
} //InitializePrintPreviewDialog

private void button1_Click(Object sender, System.EventArgs e)
{
    if (treeView1.get_SelectedNode() != null) {
        // Set the PrintDocument object's name to the selectedNode
        // object's  tag, which in this case contains the 
        // fully-qualified name of the document. This value will 
        // show when the dialog reports progress.
        document.set_DocumentName(
            treeView1.get_SelectedNode().get_Tag().ToString());
    }
    // Set the PrintPreviewDialog.Document property to
    // the PrintDocument object selected by the user.
    printPreviewDialog1.set_Document(document);
    // Call the ShowDialog method. This will trigger the document's
    //  PrintPage event.
    printPreviewDialog1.ShowDialog();
} //button1_Click

private void document_PrintPage(Object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
{
    // Insert code to render the page here.
    // This code will be called when the PrintPreviewDialog.Show 
    // method is called.
    // The following code will render a simple
    // message on the document in the dialog.
    String text = "In document_PrintPage method.";
    System.Drawing.Font printFont =
        new System.Drawing.Font("Arial", 35,
        System.Drawing.FontStyle.Regular);

    e.get_Graphics().DrawString(text, printFont,
        System.Drawing.Brushes.get_Black(), 0, 0);
} //document_PrintPage

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft