Module Level Varibles:
Private blnHeadingReq As Boolean = True
Private x As Single
Private y As Single
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
ppdPreviewReport.Document = prtReport
ppdPreviewReport.ShowDialog()
'prtReport.Print()
End Sub
Private Sub PrintHeadings(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Static intPageNo As Integer = 0
Dim fntHeadingFont As New Font("Arial", 16, FontStyle.Bold)
Dim stzStringSize As SizeF
Dim strPageNo As String
Dim sngPageNoPosition As Single
With e.Graphics
y = 25 'Set position of first line
intPageNo += 1 'Increment page number
strPageNo = "Page #" & intPageNo.ToString 'Create the page no as a string
' 'Calculate the X coordinate for centering the page no
sngPageNoPosition = CSng(e.PageBounds.Width / 2 - e.Graphics.MeasureString(strPageNo, fntHeadingFont).Width / 2)
' 'Draw the Page no on the document
.DrawString(strPageNo, fntHeadingFont, Brushes.Black, sngPageNoPosition, y)
y = 50 'Advance to the second heading line
' 'Draw the first two coloumn headings
.DrawString("String Value", fntHeadingFont, Brushes.Black, 100, y)
End With
y = 100 'Initialize y to top of page
blnHeadingReq = False 'set blnHeadingReq = false
End Sub
Private Sub prtReport_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prtReport.PrintPage
Static i As Integer = 0
Static fntPrintFont As New Font("Arial", 10, FontStyle.Regular)
Static sngPageBottom As Single = e.MarginBounds.Bottom
Static intEndOfList As Integer = ListBox1.Items.Count - 1
Static sngLineHeight As Single = fntPrintFont.GetHeight * 2 'dblspaced
Dim strString As String
Dim stzStringSize As New SizeF
Do While i <= intEndOfList And y < sngPageBottom
If blnHeadingReq Then
Call PrintHeadings(sender, e)
End If
strString = ListBox1.Items(i).ToString
e.Graphics.DrawString(strLastName, fntPrintFont, Brushes.Black, 100, y)
y += sngLineHeight 'increment y position
i += 1 'Increment to the next list item
Loop
'Check for bottom of page
If y > sngPageBottom Then
blnHeadingReq = True
e.HasMorePages = True 'Setting this causes the event to start again
' 'like a recursive call.
y = 0 'MUST RESET Y or else infinite call backs
Else
e.HasMorePages = False 'Stop executing the print
End If
End Sub