Sub GenerateReport(graphicsTarget As Object, OnForm As Boolean) Dim drawPen As Pen Dim drawBrush As SolidBrush Dim drawFont As Font 'Dim drawObject As Graphics = graphicsTarget.CreateGraphics() ' Create the graphics object for the page Dim drawString As String = "Sample Text" graphicsTarget.Clear(Color.White) 'clears the current screen If OnForm Then drawFont = New Font("Arial", 16, FontStyle.Bold) drawBrush = New SolidBrush(Color.BlueViolet) graphicsTarget.DrawString("text in a box", drawFont, drawBrush, 150, 50) drawPen = New Pen(Color.Black, 3) graphicsTarget.DrawRectangle(drawPen, 120, 50, 200, 25) End If drawPen = New Pen(Color.Purple, 5) drawFont = New Font("Calibri", 20, FontStyle.Italic) drawBrush = New SolidBrush(Color.Red) graphicsTarget.DrawString("text above a line", drawFont, drawBrush, 150, 290) graphicsTarget.DrawLine(drawPen, 150, 320, 350, 320) drawFont.Dispose() drawBrush.Dispose() 'drawObject.Dispose() End Sub
Sub WriteOnFrom() ' writes to form Dim OnForm As Boolean = True With frmOutput .Visible = True .WindowState = FormWindowState.Normal .PrintPreviewControl.Visible = False GenerateReport(frmOutput.CreateGraphics, OnForm) End With End Sub Private Sub PrintOutput_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage 'Prepares printed document Dim OnForm As Boolean = False GenerateReport(e.Graphics, OnForm) 'e.HasMorePages = False End Sub