Public Class Form1
Public CalledDirectly As Boolean = False
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' The 'If' statement below takes care of the autoclose
If Not PrintDocument1.PrintController.IsPreview AndAlso Not CalledDirectly Then
PrintPreviewDialog1.Close()
End If
e.Graphics.DrawString( "a", Me.Font, Brushes.Black, 20, 20)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Possible print during a printpreview dialog.
' If this print happens, then we autoclose the
' print preview dialog window
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Directprint after a setup dialog
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
' Any direct prints must be surrounded by
' 'CalledDirectly' True/Falses
CalledDirectly = True
PrintDocument1.Print()
CalledDirectly = False
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Immediate print with no dialog.
' Any direct prints must be surrounded by
' 'CalledDirectly' True/Falses
CalledDirectly = True
PrintDocument1.Print()
CalledDirectly = False
End Sub
End Class
|