Display Window's Page Setup Dialog from VB.NET

From .NET you can display Windows standard Page Setup Dialog using the PageSetupDialog object. While this object derives from the System.Windows.Forms namespace you need to create a PrintDocument which derives from the System.Drawing.Printing namespace.

    Imports System.Drawing.Printing

    Private myDocumentToPrint As PrintDocument
    Private myPageAlreadySetUp As Boolean = False

The PrintDocument object is the key to printing in .NET. and is similar to VB6's Printer object. Click here to learn more about using the PrintDocument object.

The PageSetupDialog object lets you display the Page Setup Dialog and control which features, such as paper orientation, printer selection, etc., are available by setting the corresponding properties. However, these settings are only in effect for the PrintDocument you associate with your PageSetupDialog object.

    Dim psd As New PageSetupDialog

    With psd
        .AllowMargins = True
        .AllowOrientation = True
        .AllowPaper = True
        .AllowPrinter = True
        .ShowHelp = True
        .ShowNetwork = True

        .Document = myDocumentToPrint
    End With

After instantiating a PageSetupDialog object you can set its default values prior to showing the dialog. Typically you would do this before showing the dialog the first time. Subsequently you would just let the dialog display the current values.

    '
    ' Set defaults on initial display.
    '
    If Not myPageAlreadySetUp Then
        With psd.Document.DefaultPageSettings
            .Margins.Top = 50
            .Margins.Left = 50
            .Margins.Right = 50
            .Margins.Bottom = 50
            .Landscape = True
        End With
    End If
    '
    ' Show the Page Setup Dialog.
    '
    psd.ShowDialog()



About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page