PrintDocument.QueryPageSettings Event

Definition

Occurs immediately before each PrintPage event.

C#
public event System.Drawing.Printing.QueryPageSettingsEventHandler QueryPageSettings;

Event Type

Examples

The following code example prints a document with the first page in color, if the printer supports it. The example requires that a PrintDocument variable named printDoc has been created, and the PrintPage and QueryPageSettings events are handled. The currentPageNumber variable is incremented after every page is printed in the PrintPage event, which is not shown.

Use the System.Drawing and System.Drawing.Printing namespaces for this example.

C#

private void MyButtonPrint_OnClick(object sender, System.EventArgs e)
{
    
    // Set the printer name and ensure it is valid. If not, provide a message to the user.
    printDoc.PrinterSettings.PrinterName = "\\mynetworkprinter";

    if (printDoc.PrinterSettings.IsValid) {
    
        // If the printer supports printing in color, then override the printer's default behavior.
        if (printDoc.PrinterSettings.SupportsColor) {

            // Set the page default's to not print in color.
            printDoc.DefaultPageSettings.Color = false;
        }

        // Provide a friendly name, set the page number, and print the document.
        printDoc.DocumentName = "My Presentation";
        currentPageNumber = 1;
        printDoc.Print();
    }
    else {
        MessageBox.Show("Printer is not valid");
    }
}

private void MyPrintQueryPageSettingsEvent(object sender, QueryPageSettingsEventArgs e)
{
    // Determines if the printer supports printing in color.
    if (printDoc.PrinterSettings.SupportsColor) {

        // If the printer supports color printing, use color.
        if (currentPageNumber == 1 ) {

            e.PageSettings.Color = true;
        }
    }    
}

Remarks

It is possible to print each page of a document using different page settings. You set page settings by modifying individual properties of the QueryPageSettingsEventArgs.PageSettings property or by setting the property to a PageSettings. Changes made to the PageSettings affect only the current page, not the document's default page settings. The print job can also be canceled by setting the Cancel property to true for the QueryPageSettingsEventArgs.

To associate the event with your event handler, add an instance of the QueryPageSettingsEventHandler delegate to the event. The event handler is called whenever the event occurs. For more information about handling events with delegates, see Handling and Raising Events.

If you use the QueryPageSettings event to modify printer settings, the performance of the PrintPreviewDialog control will not improve even if an optimization configuration switch is set. For more information, see PrintPreviewDialog control overview.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also