5

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?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Remco
  • 269
  • 3
  • 7
  • 16

6 Answers6

5

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:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
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

Bartosz
  • 3,318
  • 21
  • 31
2

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

RobinJ
  • 5,022
  • 7
  • 32
  • 61
1

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();
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
0

Umm, you can use the cleverly named PrintDialog class....

Simon
  • 6,062
  • 13
  • 60
  • 97
0

If your using WinForms to build your UI, then you can use the native PrintDialog control (see here). So far as I know, that should appear in the Toolbox in the designer mode of a WinForms control.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55