Currently, what I'm doing is this:
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();
}
}
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With