Unhandled Exception when cancelling print from PrintPreviewDialog

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My app uses PrintPreviewDialog to allow the user to preview and print. If
the user selects print from the toolbar, and the current printer settings are
set to print to a file, a "Print to File" dialog asks them to enter a
filename. If at this point they press Cancel, they get an unhandled
exception (see below). What is strange is that I have a catch for the
exception, and all seems OK when debugging, but it is not handled when the
end user does this.

Any ideas why the exception is not caught?

[XP Pro SP2, .Net 1.1 SP1, VS 2003]

Code sample:

PrintPreviewDialog dlgPreview = new PrintPreviewDialog();

try
{
dlgPreview.Document = m_printDoc;
dlgPreview.ShowDialog(this);
}
catch(Win32Exception ex)
{
// inform the user of the problem
MessageBox.Show(ex.Message, "Printing", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}

Exception Text:

System.ComponentModel.Win32Exception: The operation was canceled by the user
at System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint
(PrintDocument document, PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at System.Windows.Forms.PrintPreviewDialog.ToolBarClick(Object source,
ToolBarButtonClickEventArgs eventargs)
at System.Windows.Forms.ToolBar.OnButtonClick(ToolBarButtonClickEventArgs
e)
at System.Windows.Forms.ToolBar.WmReflectCommand(Message& m)
at System.Windows.Forms.ToolBar.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
 
Hi sigee,

Thanks for your post.

After doing some search in our internal database, I found that this is a
known issue, which has confirmed by our product team.

To workaround this issue, we can use the workaround below:

1) Either override OnQueryPageSettings if you have a custom PrintDocument
or hook on to the event itself. 2) In your PrintController.OnStartPrint,
when you find the user has cancelled the print, store the fact away in a
bool member variable or set a property on the custom PrintDocument to let
it know. Let's call this UserCancelled. 3) While handling the
QueryPageSettings event, set e.Cancel = UserCancelled.

If with this workaround you still can not get rid of the problem, I suggest
you provide a simple sample project for me to reproduce the problem on my
side. Then I can understand this issue much better. Thanks
=========================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for your reply Jeffrey - much appreciated.

I got the suggested workaround to work, although it did entail me having to
subclass a PrintController in order to override the OnStartPrint, catch the
exception and set the flag.

Thanks again,
sigee
 
Hi sigee,

I am glad you finally got it to work :-)

Yes, we have to do these extra work to workaround the issue. Because
internally in PrintPreviewDialog class, our product team did not handle
this cancel Win32Exception, we have to handle it manually ourselves.

If you have any other concern, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffrey,

I have the same print issue which sigee had. But the scenario is little
different. I am using the PrintPreview method of the Infragistics grid
to print the grid contents. I can understand from your reply that some
work around id required. However, I am not using any other print
components. Could you please suggest something for me? I would
appreciate your reply.

My code loo like this:

if(PrinterSettings.InstalledPrinters.Count > 0 )
{
if(hasGridRows)
{
Infragistics.Win.UltraWinGrid.UltraGridLayout printLayout =
StatusReportGrid.DisplayLayout.Clone();
printLayout.Override.RowAlternateAppearance.BackColor =
Color.White;
printLayout.Override.RowAlternateAppearance.BorderColor =
Color.White;
printLayout.Override.RowAppearance.BorderColor = Color.White;
printLayout.Override.HeaderAppearance.BackColor = Color.White;
printLayout.Override.HeaderAppearance.BorderColor =
Color.White;
printLayout.Override.HeaderAppearance.FontData.Bold =
Infragistics.Win.DefaultableBoolean.True;
printLayout.Override.RowSelectors =
Infragistics.Win.DefaultableBoolean.False;
StatusReportGrid.PrintPreview(printLayout,Infragistics.Win.UltraWi
nGrid.RowPropertyCategories.All);

}
}

Thanks & Best Regards,
Mani
 
Back
Top