Matthias Wandel
Matthias Wandel

Reputation: 6483

Disabling "print" button in .net print preview dialog

I'm working on a C# / .net app. I want the user to be able to print preview, but I don't want the user to be able to print from the print straight from the preview dialog.

The print preview dialog has a little printer button on it that sends the previewed pages straight to the printer. Question is, is there a way to get rid of / disable / intercept this button click?

Upvotes: 5

Views: 14399

Answers (5)

IdivideBy0
IdivideBy0

Reputation: 51

Thank you all for this thread. I just wanted to share another method built upon Ion Roata's answer above. Over ride the base class with a custom PrintPreviewDialog class. Makes for cleaner looking code JMHO.

class customPrintPreviewDialog : PrintPreviewDialog
{

   public customPrintPreviewDialog() //default constructor
    {
        // over ride the print button default enabled property
       ((ToolStripButton)((ToolStrip)this.Controls[1]).Items[0]).Enabled = false;
    }

   // Add more of your customization here.

}

And the code to instantiate...

 customPrintPreviewDialog objCPPdialog = new customPrintPreviewDialog();

Upvotes: 4

Ion Roata
Ion Roata

Reputation: 286

You can access the print button, and any other button from a print preview control by searching in the container's controls collection.

For the print button:

(ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]

so, to disable it,

((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Enabled = false;

Upvotes: 14

mathias.horn
mathias.horn

Reputation: 251

In VB I use this, use a code converter to C#:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items(0).Enabled = False

    PrintPreviewDialog1.ShowDialog()

End Sub

Upvotes: 2

Wirah
Wirah

Reputation: 20

this.reportViewer1.ShowPrintButton = False;

Upvotes: -3

Steve Ellinger
Steve Ellinger

Reputation: 3973

The PrintPreviewDialog class is actually a wrapper around the PrintPreviewControl class and it is what is supplying the buttons in the toolbar. Any form can host the PrintPreviewControl so what you would have to do is host the PrintPreviewControl in a dialog form you create:

public partial class PreviewDialog : Form
{
    public PreviewDialog() {
        this.printPreviewControl1 = new System.Windows.Forms.PrintPreviewControl();
        this.SuspendLayout();
        // 
        // printPreviewControl1
        // 
        this.printPreviewControl1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.printPreviewControl1.Location = new System.Drawing.Point(0, 0);
        this.printPreviewControl1.Name = "printPreviewControl1";
        this.printPreviewControl1.Size = new System.Drawing.Size(292, 273);
        this.printPreviewControl1.TabIndex = 0;
        this.printPreviewControl1.Columns = 1;
        this.printPreviewControl1.Zoom = 1.0;
    }

}

The Columns property which is currently being set to 1 is the number of pages displayed by the control horizontally across the screen. The Zoom property sets the scale of the pages, 1.0 being full page; so < 1.0 would be a reduced image and > 1.0 would be an expanded image in the control per page. What you would want to do to the PreviewDialog class above is add a System.Windows.Forms.ToolStrip to it and then add buttons to handle the zoom, and pages per the properties mentioned (Columns and Zoom).

In the form that will bring the preview up (not the PreviewDialog form) you would have code like the following:

    private void buttonPrintPreview_Click(object sender, EventArgs e) {
        PreviewDialog dlg = new PreviewDialog();
        dlg.ShowDialog();
        return;
    }

Hopes this helps

Upvotes: 3

Related Questions