views:

121

answers:

2

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();
   }
}
+1  A: 

Since you have access to the buttons in the toolstrip, remove the old print button and add your own. Assign the image from the default print button and you are all set. The code woudl look something like this:

ts.Items.Remove(printButton);
ToolStripButton b = new ToolStripButton();
b.ImageIndex = printButton.ImageIndex;
b.Visible = true;
ts.Items.Insert(0, b);
b.Click += new EventHandler(this.SelectPrinterAfterPreview);
anchandra
Perfect! That worked like a charm. I was stuck in the mindset of working with their button, overlooking the much simpler idea of replacing it. Thanks, anchandra!
C. Griffin
A: 

I think replace buttons or use the Control Names from PrintPreviewDialog is´nt a good option.

From Net1 to Net2 changes the name for the ToolBar. Next version can also change it or the name for other controls.

The PrintPreviewDialog is a very simple Form to encapsulate PrintPreviewControl.

You can build a new Form and put own buttons and implement your funcionality.

You can find some Dialogs for PrintPreview at Code-Project (CoolPrintPreviewDialog) An Enhaced PrintPreviewDialog.

On my PrvDialog, when user press the Print Button I show a PageSelDialog to allow the user select Range to Print (Current page, Some Pages, All-Pages, Cancel).

Other solution is override OnBeginPrint / suscribe event BeginPrint from PrintDocument. Here you can show the PageSelDialog, cancel the Print and alter the DefaultPageSettings PrintRange, FromPage, ToPage.

For this Option you need know when is PrintToPrinter, Preview or Print From PrintButon. PrintController.IsPreview, resolve for Preview Option.

x77