Remco
Remco

Reputation: 279

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?

Upvotes: 5

Views: 61077

Answers (6)

Fischermaen
Fischermaen

Reputation: 12468

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

Upvotes: 1

Heinzi
Heinzi

Reputation: 172448

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:

Upvotes: 5

RobinJ
RobinJ

Reputation: 5273

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

Upvotes: 2

Bartosz
Bartosz

Reputation: 3358

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

Upvotes: 2

Samuel Slade
Samuel Slade

Reputation: 8623

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.

Upvotes: 0

Simon
Simon

Reputation: 6152

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

Upvotes: 0

Related Questions