5

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
  • 6,383
  • 10
  • 33
  • 31

5 Answers5

14

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;
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Ion Roata
  • 286
  • 3
  • 6
4

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();
IdivideBy0
  • 51
  • 1
3

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

Steve Ellinger
  • 3,957
  • 21
  • 19
1

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
khr055
  • 28,690
  • 16
  • 36
  • 48
mathias.horn
  • 241
  • 1
  • 8
  • 12
-3

this.reportViewer1.ShowPrintButton = False;

Wirah
  • 20
  • 3