-
Apr 7th, 2018, 08:20 AM
#1
Thread Starter
Lively Member
[RESOLVED] close Print Preview after print ?
hello
what is code to close Print Preview after print ?
here my code
Code:
Public Class Form12
Private WithEvents PD As New Printing.PrintDocument
Private PPD As New PrintPreviewDialog With {.WindowState = FormWindowState.Normal}
Private Sub button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
Button8.Visible = False
Button7.Visible = False
Me.Hide()
PPD.Document = PD
PPD.ShowDialog()
' Me.Visible = True
Button8.Visible = True
Button7.Visible = True
End Sub
Private Sub PD_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage
Dim Bmp As New Bitmap(Me.Width, Me.Height)
Me.DrawToBitmap(Bmp, New Rectangle(0, 0, Bmp.Width, Bmp.Height))
e.Graphics.DrawImage(Bmp, 0, 0)
End Sub
End Class
-
Apr 7th, 2018, 09:37 AM
#2
Re: close Print Preview after print ?
As far as I can tell, there is nothing intended for that purpose. It makes sense that the dialogue can stay open because the user may want to print again. They could have added a Boolean property that specifies whether to close after printing but I guess they figured that that should be the user's choice rather than the developer's.
That said, I just tested your code with the following snippet added to the end of the PrintPage event handler and it seemed to work as you want:
vb.net Code:
If Not PD.PrintController.IsPreview Then 'This was the real print run. PPD.Close() End If
That will have no effect when the event handler is executed the first time to generate the preview but when the second, "real" print run completes, it will close the dialogue.
-
Apr 7th, 2018, 10:24 AM
#3
Thread Starter
Lively Member
Re: close Print Preview after print ?
-
Mar 13th, 2021, 05:46 AM
#4
New Member
Re: [RESOLVED] close Print Preview after print ?
Originally Posted by kankon
hello
what is code to close Print Preview after print ?
I just wrote a solution in C#, hope it also works in VB.
A Preview.Close() command after Preview.ShowDialog() can never be reached,
because you have to close the Preview in order to reach it. So you must
call Preview.Close() from somewhere else.
When printing starts with document.Print, document_StartPrint is called,
when printing is finished, document_EndPrint is called. It seems that
Preview.ShowDialog does internally call document.Print, but the output
is redirected to the preview window. When you click the printer icon in
Previwe, document.Print is called again, this time with the selected
printer.
If you put the Preview.Close() command in the EndPrint function, it is
called from outside the Preview and closes. But you must not close while
the preview is being produced, so EndPrint must know when this is the
case.
The solution which works for me is:
Declare the following global variables:
private static bool InPreview;
private static PrintPreviewDialog Preview;
Add the EndPrint function to your document, and gefore calling the dialog,
set InPreview to true:
PrintDialog PD = new PrintDialog();
...
doc = new PrintDocument();
doc.EndPrint += new PrintEventHandler(doc_EndPrint);
...
if (PD.ShowDiadog() == DialogResult.OK) {
...
InPreview = true;
Preview.ShowDialog();
}
The EndPrint function looks like this:
private static void doc_EndPrint(object sender, PrintEventArgs e) {
if(InPreview) {
InPreview = false;
} else {
if(Preview != null) {
Preview.Close();
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|