Results 1 to 4 of 4

Thread: [RESOLVED] close Print Preview after print ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    102

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. If Not PD.PrintController.IsPreview Then
    2.     'This was the real print run.
    3.     PPD.Close()
    4. 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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    102

    Re: close Print Preview after print ?

    thank you very much

  4. #4
    New Member
    Join Date
    Mar 2021
    Posts
    1

    Re: [RESOLVED] close Print Preview after print ?

    Quote Originally Posted by kankon View Post
    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
  •  



Click Here to Expand Forum to Full Width