Export (0) Print
Expand All

Determining Page Properties Using the PageSetupDialog Component

Visual Studio .NET 2003

The PageSetupDialog component presents layout, paper size, and other page layout choices to the user for a document.

You need to specify an instance of the PrintDocument class — this is the document to be printed. Additionally, users must have a printer installed on their computer, either locally or through a network, as this is partly how the PageSetupDialog component determines the page formatting choices presented to the user.

An important aspect of working with the PageSetupDialog component is how it interacts with the PageSettings class. The PageSettings class is used to specify settings that modify the way a page will be printed, such as paper orientation, the size of the page, and the margins. Each of these settings is represented as a property of the PageSettings class. The PageSetupDialog class modifies these property values for a given instance of the PageSettings class that is associated with the document (and is represented as a PrintDocument.DefaultPageSettings property).

To set page properties using the PageSetupDialog component

  • 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 PageSetupDialog component. An existing document is specified in the Document property, and its Color property is set to false.

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

    ' 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.
       PageSetupDialog1.Document = myDocument
       ' Sets the print document's color setting to false,
       ' so that the page will not be printed in color.
       PageSetupDialog1.Document.DefaultPageSettings.Color = False
       PageSetupDialog1.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.
       pageSetupDialog1.Document = myDocument;
       // Sets the print document's color setting to false,
       // so that the page will not be printed in color.
       pageSetupDialog1.Document.DefaultPageSettings.Color = false;
       pageSetupDialog1.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.
          pageSetupDialog1->Document = myDocument;
          // Sets the print document's color setting to false,
          // so that the page will not be printed in color.
          pageSetupDialog1->Document->DefaultPageSettings->Color = false;
          pageSetupDialog1->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

Creating Standard Windows Forms Print Jobs | PageSetupDialog Component (Windows Forms) | PageSetupDialog Class

Show:
© 2014 Microsoft