C# PrintPreviewDialog Modification possible?

Posted by C. Griffin on Stack Overflow See other posts from Stack Overflow or by C. Griffin
Published on 2010-05-12T18:22:19Z Indexed on 2010/05/12 18:24 UTC
Read the original article Hit count: 717

Filed under:
|
|
|
|

Currently, what I'm doing is this:

  1. Using the built-in .NET PrintPreviewDialog
  2. Attaching my own Click handler to the Print button, which allows for the user to select the printer before finally printing.

This all WORKS, HOWEVER, the OnprintToolStripButtonClick event is still sending the document to the default printer BEFORE the user gets to choose the actual printer and click Print (which works, but they're getting an extra copy on the default printer first from the old Handler).

Can I remove this built-in Click handler? I've tried the other methods mentioned on here in regards to using an EventHandlerList to remove the handlers, but it doesn't work for the built-in printing event. Here is a copy of my current code in case it helps clarify:

// ... Irrelevant code before this
private PrintPreviewDialog ppdlg;

ToolStrip ts = new ToolStrip();
ts.Name = "wrongToolStrip";
foreach (Control ctl in ppdlg.Controls)
{
   if (ctl.Name.Equals("toolStrip1"))
   {
      ts = ctl as ToolStrip;
      break;
   }
}
ToolStripButton printButton = new ToolStripButton();
foreach (ToolStripItem tsi in ts.Items)
{
   if (tsi.Name.Equals("printToolStripButton"))
   {
      printButton = tsi as ToolStripButton;
   }
}
printButton.Click += new EventHandler(this.SelectPrinterAfterPreview);
// ... Irrelevant code afterwards omitted


// Here is the Handler for choosing a Printer that gets called after the
// PrintPreviewDialog's "Print" button is clicked.
private void SelectPrinterAfterPreview(object sender, EventArgs e)
{
   frmMainPage frmMain = (frmMainPage)this.MdiParent;
   if (frmMain.printDialog1.ShowDialog() == DialogResult.OK)
   {
      pd.PrinterSettings.PrinterName = frmMain.printDialog1.PrinterSettings.PrinterName;
      pd.PrinterSettings.Copies = frmMain.printDialog1.PrinterSettings.Copies;
      pd.Print();
   }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about printing