Disabling "print" button in .net print preview dialog

13,404

Solution 1

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

Solution 2

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;

Solution 3

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

Solution 4

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
Share:
13,404
Matthias Wandel
Author by

Matthias Wandel

Updated on June 05, 2022

Comments

  • Matthias Wandel
    Matthias Wandel about 2 years

    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?

  • Matthias Wandel
    Matthias Wandel almost 14 years
    Seems like the right direction. Unfortunately, I don't at this point have the background to turn the above into working code.
  • Ali Tavakoli
    Ali Tavakoli over 13 years
    where does he say he is using reporting services?