I'm hoping here is a simple answer that my lack of programming skills are hiding from me... I generate reports that I'd like to immediately display on a FORM for the Users review and acceptance. Once accepted this can be PRINTED. The "on-form" report is a subset of the full report that is included in the printed document (includes logo's, border graphics and more detail text, etc), so most of the generation of the document is the same, but not all. Rather than duplicate all the report code twice, once for the form and again for printing I hope there is a way to generate a common subroutine that both the form report and printing routines can call and that contains the conditional formatting lines.
For writing to the Form:
Module DisplayCode
Sub WriteOnFrom()
' writes to form
With frmOutput
.Visible = True
.WindowState = FormWindowState.Normal
.PrintPreviewControl.Visible = False
Dim drawPen As Pen
Dim drawBrush As SolidBrush
Dim drawFont As Font
Dim drawObject As Graphics = .CreateGraphics() 'Create object for the page
Dim drawString As String = "Sample Text"
drawObject.Clear(Color.White) 'clears the current screen
drawFont = New Font("Arial", 16, FontStyle.Bold)
drawBrush = New SolidBrush(Color.BlueViolet)
drawObject.DrawString("text in a box", drawFont, drawBrush, 150, 50)
drawFont = New Font("Calibri", 20, FontStyle.Italic)
drawBrush = New SolidBrush(Color.Red)
drawObject.DrawString("text above a line", drawFont, drawBrush, 150, 290)
drawPen = New Pen(Color.Black, 3)
drawObject.DrawRectangle(drawPen, 120, 50, 200, 25)
drawPen = New Pen(Color.Purple, 5)
drawObject.DrawLine(drawPen, 150, 320, 350, 320)
drawFont.Dispose()
drawBrush.Dispose()
drawObject.Dispose()
End With
End Sub
End Module
For the PrintDocument:
Public Class PrintOutput
Inherits System.Drawing.Printing.PrintDocument
Private Sub PrintOutput_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage
'Prepares printed document
Dim drawPen As Pen
Dim drawBrush As SolidBrush
Dim drawFont As Font
Dim drawString As String = "Sample Text"
'Can I move this to a subroutine outside the Class?
drawFont = New Font("Arial", 16, FontStyle.Bold)
drawBrush = New SolidBrush(Color.BlueViolet)
e.Graphics.DrawString("text in a box", drawFont, drawBrush, 150, 50)
drawFont = New Font("Calibri", 20, FontStyle.Italic)
drawBrush = New SolidBrush(Color.Red)
e.Graphics.DrawString("text above a line", drawFont, drawBrush, 150, 290)
drawPen = New Pen(Color.Black, 3)
e.Graphics.DrawRectangle(drawPen, 120, 50, 200, 25)
drawPen = New Pen(Color.Purple, 5)
e.Graphics.DrawLine(drawPen, 150, 320, 350, 320)
e.HasMorePages = False
drawFont.Dispose()
drawBrush.Dispose()
drawPen.Dispose()
End Sub
End Class
Of course, this the same report for both for now, so just an example to demo the principles.
What I have tried:
Of course its possible to use the PrintPreview control on my frmOutput and develop this all as a PrintDocument object but I'd rather not. Firstly, it takes a while to generate this (on-form is instantaneous), secondly I'm not interested in the paper size format etc and just fit it onto a fixed size frmOutput and finally, I still don't know how to generate a subset of this report for the PrintPreview control relative to the full report for printing.
BTW for the form report it appears I still have to use .CreateGraphics() to write to the form itself, which is a conflict to simply using e.Graphics. for PrintDocument. I'm not especially committed to writing directly to the form, so any control placed on the form that accepts text & graphis will do.
Any great suggestions?