I've been coding with VB for a short time and feel I have a pretty good grasp so far, but the PrintDocument class has my hair turning gray.
My goal is to query a MySQL table and extract data to display in a form. The data will span multiple pages.
What I have in brief is:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim newMargins As System.Drawing.Printing.Margins
newMargins = New System.Drawing.Printing.Margins(20, 20, 20, 20)
Me.PrintDocument1.DefaultPageSettings.Margins = newMargins
Me.PrintDocument1.DefaultPageSettings.Landscape = True
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.OriginAtMargins = True
. . .
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim PrintFont As New Font("Arial", 10)
Dim PrintFontBold As New Font("Arial", 10, FontStyle.Bold)
Dim PrintFontItalic As New Font("Arial", 10, FontStyle.Italic)
Dim HeadingFont As New Font("Arial", 18, FontStyle.Bold)
Dim LineHeightSingle As Single = PrintFont.GetHeight + 2
Dim LineHeightDouble As Single = (PrintFont.GetHeight * 2) + 2
Dim ColumnHorizontalLocationSingle As Single = e.MarginBounds.Left
Dim PrintLineString As String = ""
Dim FontSizeF As New SizeF
Dim VerticlePrintLocation As Single = e.MarginBounds.Top
Dim iPageCount as Integer = 1
PrintLineString = "InOutBoard Detail Report"
e.Graphics.DrawString(PrintLineString, HeadingFont, Brushes.Black, ColumnHorizontalLocationSingle, VerticlePrintLocation)
VerticlePrintLocation += LineHeightSingle
PrintLineString = "Page No. " & iPageCount.toString()
e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, ColumnHorizontalLocationSingle, VerticlePrintLocation)
VerticlePrintLocation += LineHeightDouble
. . .
[ SQL QUERY HERE ]
. . .
' Check for end of page
If VerticlePrintLocation > e.PageBounds.Bottom Then
e.HasMorePages = True
e.HasMorePages = False
==== QUESTIONS =========================================
Here are my questions. First off when my verticleprintlocation exceeds e.pagebounds (confirmed because I had them print on the report), e.HasMorePages is not set to True. The data continues to print through the bottom of the page.
Secondly, I understand now that it is this routine that is printed for each page, which means I have to requery my MySQL table for each page, asking for data starting with where the last page left off, correct? So do I just keep track of id numbers in the table and query for starting at the next highest id? Does anyone have any snippets of code for me to look at regarding VB / PrintDocument / MySQL? This shouldn't be that difficult but for some reason I just can't wrap my head around this one.