Canceling Print using PrintDocument object

R

rbuzzard1

Greetings all, i am hoping you can help me. I have a process that
utilizes the PrintDocument object to print a set of pages. I know that
the PrintDocument object, once you call the PrintDocument.Print()
method will repeatedly fire the PrintPage event for each page to be
printed. When that happens, a windows printing dialog displays showing
a simple message like 'Printing Page 1' along with a Cancel button.
The PrintPage event is passed in a set of PrintPageEventArgs that
includes a 'Cancel' property.

I created a PrintPage event handler method that looks something like
this:

private void pd_PrintPage(object sender, PrintPageEventArgs e) {
if (e.Cancel)
{
if (CancelPrint != null)
{
CancelPrint();
}
return;
}

//do some stuff
}

Now, as you can see, i am checking to determine if the Cancel button
is pressed on the dialog, and if so, i call a method to do some work
when the user cancels the printing process. This works, but only part
of the time. What i have found is that, depending on the timing, if
you click the Cancel button on the Printing dialog at just the right
time, the PrintPage event does not get fired, and the process goes
straight to firing the PrintDocument.EndPrint event. The EndPrint
event handler takes a set of PrintEventArgs including a 'Cancel'
property. The problem with this is that when that happens, the Cancel
property on the PrintEventArgs is always set to false, even when the
Cancel button was pressed.

To reiterate, part of the time when clicking cancel, the PrintPage
event fires, and the cancel property of the PrintPageEventArgs is
true, and i can handle the cancel as i want. However, at other times,
when clicking cancel, the PrintPage event does not fire, the document
stops printing, and the PrintEnd event fires, but the PrintEventArgs
contains a cancel property of false, even though the cancel button was
pressed.

I would like to know first of all why this behavior is happening.
Also, can anyone offer suggestions as to how I can ensure that I am
able to handle when the Cancel button is pressed? Keep in mind that
the print dialog that is displayed is the result of the internal
process within the PrintDocument.Print() method, so I don't have
access to the controls, etc, except those that are exposed in the
PrintDocument object.

I'm totally stumped! Thanks for any help...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top