How can I use the Print Dialog

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?

81.4k 14 14 gold badges 158 158 silver badges 230 230 bronze badges asked Nov 8, 2011 at 14:04 279 3 3 gold badges 7 7 silver badges 16 16 bronze badges

6 Answers 6

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:

1 1 1 silver badge answered Nov 8, 2011 at 14:06 171k 59 59 gold badges 377 377 silver badges 541 541 bronze badges answered Nov 8, 2011 at 14:07 3,358 22 22 silver badges 31 31 bronze badges

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(); >> 
answered Nov 8, 2011 at 14:11 5,162 7 7 gold badges 35 35 silver badges 62 62 bronze badges

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();