目前,我正在做的是:
- 使用内置的 .NET PrintPreviewDialog
- 将我自己的 Click 处理程序附加到 Print 按钮,它允许用户在最终打印之前选择打印机。
这一切都有效,但是,在用户选择实际打印机并单击打印之前, OnprintToolStripButtonClick 事件仍在将文档发送到默认打印机(这有效,但他们首先在默认打印机上从旧的处理程序)。
我可以删除这个内置的 Click 处理程序吗?我已经尝试过这里提到的关于使用 EventHandlerList 删除处理程序的其他方法,但它不适用于内置打印事件。这是我当前代码的副本,以防它有助于澄清:
// ... 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();
}
}