How can I use the Print Dialog

52,857

Solution 1

This dialog box is a so-called common dialog, a built-in Windows dialog that can be used by multiple applications.

To use this dialog box in your C# application, you can use the PrintDialog class. The following MSDN pages contains descriptions as well as some sample code:

Solution 2

If you use WPF, you may use PrintDialog: http://msdn.microsoft.com/en-us/library/system.windows.controls.printdialog.aspx

if you're into WinForms you may use...PrintDialog: http://msdn.microsoft.com/en-us/library/system.windows.forms.printdialog.aspx

Solution 3

For the CTRL+P shortcut: Add a toolbar (I think it was called ToolStrip) to your form, put an entry in it to wich you assign the shortcut CTRL+P from the properties panel. For the PrintDialog: Add a PrintDialog control to your form and set the Document property to the document that should be printed. Go into the code for the click event of your print entry in the toolbar. Add the code PrintDialog.ShowDialog(); to it, check if the Print button was clicked, and if so, print it using DocumentToPrint.Print();. Here's an example:

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

        // Allow the user to choose the page range he or she would
        // like to print.
        PrintDialog1.AllowSomePages = true;

        // Show the help button.
        PrintDialog1.ShowHelp = true;

        // Set the Document property to the PrintDocument for 
        // which the PrintPage Event has been handled. To display the
        // dialog, either this property or the PrinterSettings property 
        // must be set 
        PrintDialog1.Document = docToPrint;

        DialogResult result = PrintDialog1.ShowDialog();

        // If the result is OK then print the document.
        if (result==DialogResult.OK)
        {
            docToPrint.Print();
        }

    }

Example source: http://msdn.microsoft.com/en-us/library/system.windows.forms.printdialog.document.aspx

Solution 4

You can have a standard print dialog with this:

var printDialog = new PrintDialog();
printDialog.ShowDialog();

... but printing has to be done by yourself ... ;-)

Edit: For all those who still use VisualStudio2005:

PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
Share:
52,857
Remco
Author by

Remco

Updated on February 09, 2020

Comments

  • Remco
    Remco over 4 years

    If you go in Visual Studio 2005 to the following (or just do ctrl+p): File ==> Print..

    You get a print dialog screen. I want the same in my program, but how?