如果我运行此代码,并在PrintDialog上按取消,它仍会打印。如何判断使用是否取消?
PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();
dialog.ShowDialog();
document.PrinterSettings = p.PrinterSettings;
document.Print();
附录
WebBrowser w = new WebBrowser();
w.ShowPrintDialog(); //.ShowPrintDialog returns a void, how can I deal with this?
答案 0 :(得分:7)
您可以检查ShowDialog方法的结果:
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//Print
}
答案 1 :(得分:3)
ShowDialog返回对话框结果枚举。它可以是OK,也可以是Cancel。
PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();
if(dialog.ShowDialog() == DialogResult.Ok)
{
document.PrinterSettings = p.PrinterSettings;
document.Print();
}
答案 2 :(得分:0)
以上答案适用于System.Windows.Forms.PrintDialog
。但是,如果您未构建Forms
应用程序,则您将使用的PrintDialog
为System.Windows.Controls.PrintDialog
。在此处,ShowDialog
会返回bool?
:
var dialog = new System.Windows.Controls.PrintDialog();
if (dialog.ShowDialog() == true)
{
// Print...
}