If the user clicks the Cancel button while the Generating Preview window is displayed, I would like to
a) Print something like * Terminated * at the end of the report, and
b) notify the calling routine that the PrintDocument was cancelled.
I have been able to work around (b) by adding a WasCancelled property to the report class...
Private mbWasCancelled As Boolean
Public ReadOnly Property WasCancelled() As Boolean
Get
Return mbWasCancelled
End Get
End Property
Private Sub PrintPage(ByVal sender As System.Object, ByVal e As Printing.PrintPageEventArgs) Handles moDocument.PrintPage
Do While CurrentLine < LinesToBePrinted
... write the line
CurrentLine += 1
Loop
e.HasMorePages = (CurrentLine < LinesToBePrinted)
End Sub
Private Sub moDocument_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles moDocument.EndPrint
mbWasCancelled = (CurrentLine < LinesToBePrinted)
End Sub
This assumes that if the EndPrint event occurs before all the lines are printed it must be because the user cancelled the dialog. This seems a bit of a kludge. Is there a better way?
With regard to printing some Termination text on the report: My observation has been that the EndPrint event is not actually raised until the completion of the current PrintPage code (so the balance of the page is still generated), and then in the EndPrint event the Page object seems to reject any further attempts to write to it.
So what am I meant to do? Use the WasCancelled flag within the PrintPage routine to print the terminated message on the next page and then set e.HasMorePages to false? This would kind of work except it would be nice to print the message mid-way down the current page, rather than on the next page on its own.