HasMorePages Issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm having trouble getting e.hasmorepages to print to another page. It keeps printing the same page with the text for the second page starting on the top, and repeats for an unlimited number of pages. I'd post my code if someone will be willing to do a code review. I have a MSDN subscription. Thanks
 
Hi,

Post your code.

Ken
-----------------------
dls said:
I'm having trouble getting e.hasmorepages to print to another page. It
keeps printing the same page with the text for the second page starting on
the top, and repeats for an unlimited number of pages. I'd post my code if
someone will be willing to do a code review. I have a MSDN subscription.
Thanks.
 
Here is the code segment. I am printing from several datareaders. The first page formats great. After the first page, the data dumps on the same page, but continues to add pages. Also, the output from the database text fields can be as much as 2000 char. Any advice on using measurestring function would be appreciated

Thanks for your help

#Region "Print TreatmentPlan

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Clic
Dim dlg As New PrintPreviewDialo
dlg.Document = mDo
dlg.WindowState = FormWindowState.Maximize
dlg.ShowDialog(
End Su

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Clic
Dim dlgPrint As New PrintDialo
dlgPrint.Document = mDo
If dlgPrint.ShowDialog = DialogResult.OK Then mDoc.Print(
End Su

Private Sub mDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mDoc.PrintPag

Dim xPos As Single = e.MarginBounds.Lef
Dim yPos As Single = e.MarginBounds.To
Dim line8Heigth As Single = mFont8.GetHeight(e.Graphics
Dim line10Heigth As Single = mFont10.GetHeight(e.Graphics
Dim line12Heigth As Single = mFont12.GetHeight(e.Graphics
Dim bBrush As Brush = Brushes.Blac
Dim lineCount As Integer =
Dim sSCCLogo As String = sImagePath & "scc_seal.jpg
Dim fi As New System.IO.FileInfo(sPhoto

With e.Graphic
.DrawImage(Image.FromFile(sSCCLogo), xPos, yPos, 172, 122
xPos = 42
yPos = e.MarginBounds.To
.DrawString("DSHS SPECIAL COMMITTMENT CENTER", mFont12, bBrush, xPos, yPos
yPos += line12Heigt
xPos = 42
lineCount +=
.DrawString("Resident Trimester Treatment Plan", mFont12, bBrush, xPos, yPos
yPos += line12Heigt
lineCount +=
.DrawString("Trimester: " & sTrimester, mFont12, bBrush, xPos, yPos
yPos += line12Heigt
.DrawString("Date: " & Date.Today.ToShortDateString, mFont12, bBrush, xPos, yPos

If fi.Exists The
xPos = e.MarginBounds.Right - 12
.DrawImage(Image.FromFile(sPhoto), xPos, yPos, 120, 130
Els
xPos = 50
yPos = 25
.DrawString("Resident Photo Unavailable", mFont10, bBrush, xPos, yPos
End I

xPos = e.MarginBounds.Lef
yPos = 25
.DrawString("Resident: " & sResident, mFont12, bBrush, xPos, yPos
yPos += line12Heigt
.DrawString("Committment Status: " & sCommitmentStatus, mFont12, bBrush, xPos, yPos
yPos += line12Heigt
.DrawString("Treatment Phase: " & iPhaseID, mFont12, bBrush, xPos, yPos
yPos += line12Heigt
.DrawString("Privilege Level: " & sPrivilegeLevel, mFont12, bBrush, xPos, yPos
yPos += line12Heigt
.DrawString("Plan Coordinator: " & sEvaluator, mFont12, bBrush, xPos, yPos
yPos += line12Heigt
.DrawString("Victim Profile: " & sVictimProfile, mFont12, bBrush, xPos, yPos
yPos += line12Heigt
.DrawString("Violence History: " & sViolenceHistory, mFont12, bBrush, xPos, yPos
yPos += line12Heigt
yPos = 45
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos
yPos += line10Heigt
'yPos += line12Heigt
.DrawString("RESIDENT DIAGNOSES", mFont12, bBrush, xPos, yPos
yPos += line10Heigt
yPos += line10Heigt
.DrawString("AXIS", mFont10, bBrush, xPos, yPos
.DrawString("DSMCode", mFont10, bBrush, xPos + 50, yPos
.DrawString("Diagnosis", mFont10, bBrush, xPos + 125, yPos
.DrawString("Defined", mFont10, bBrush, xPos + 425, yPos
.DrawString("Evaluator", mFont10, bBrush, xPos + 525, yPos
yPos += line10Heigt

Dim cmdDX As New SqlCommand("sp_GetResidentDiagnoses", sqlCN
With cmdD
.CommandType = CommandType.StoredProcedur
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumbe
End Wit
Tr
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open(
Dim drDX As SqlDataReader = cmdDX.ExecuteReader(CommandBehavior.CloseConnection
Do While drDX.Rea
.DrawString(drDX("Axis"), mFont10, bBrush, xPos, yPos
.DrawString(drDX("DSMCode"), mFont10, bBrush, xPos + 50, yPos
.DrawString(drDX("Diagnosis"), mFont10, bBrush, xPos + 125, yPos)
.DrawString(drDX("DateDefined"), mFont10, bBrush, xPos + 425, yPos)
.DrawString(drDX("ForensicEvaluator"), mFont10, bBrush, xPos + 525, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdDX.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try

yPos += line10Heigth
yPos += line10Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line10Heigth
yPos += line10Heigth
.DrawString("CLASS COMPLETIONS", mFont12, bBrush, xPos, yPos)
.DrawString("GROUP COMPLETIONS", mFont12, bBrush, 425, yPos)
yPos += line10Heigth
Dim yGroup As Single = yPos

Dim cmdCC As New SqlCommand("sp_GetResidentClassCompletions", sqlCN)
With cmdCC
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
End With

Try
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drCC As SqlDataReader = cmdCC.ExecuteReader(CommandBehavior.CloseConnection)
Do While drCC.Read
.DrawString(drCC("Opportunity"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdCC.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try
Dim yClassEndPos As Single = yPos
yPos = yGroup
Dim cmdGP As New SqlCommand("sp_GetResidentGroupCompletions", sqlCN)
With cmdGP
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
End With

xPos = 425
Try
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drGP As SqlDataReader = cmdGP.ExecuteReader(CommandBehavior.CloseConnection)
Do While drGP.Read
.DrawString(drGP("Opportunity"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdCC.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try

'set the position to the last written line
xPos = e.MarginBounds.Left
If yPos > yClassEndPos Then
yPos += line10Heigth
Else
yPos = yClassEndPos + line10Heigth
End If

'Start on the next page
e.HasMorePages = True

'Set the position to upper left of page
xPos = e.MarginBounds.Left
yPos = e.MarginBounds.Top

Dim cmdTP As New SqlCommand("sp_GetResidentTreamentPlan", sqlCN)
With cmdTP
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
.Parameters.Add("@Trimester", SqlDbType.Char, 6).Value = sTrimester
End With

Try
Dim sF As SizeF
Dim fmt As StringFormat
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drTP As SqlDataReader = cmdTP.ExecuteReader(CommandBehavior.CloseConnection)
Do While drTP.Read
'print page heading
.DrawString("DSHS SPECIAL COMMITTMENT CENTER", mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Resident Trimester Treatment Plan: " & sResident, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

'Print data
.DrawString("Domain Problem: " & drTP("DomainProblem"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
yPos += line10Heigth

.DrawString("Evidenced by behavior: ", mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
yPos += line10Heigth

sF = e.Graphics.MeasureString(drTP("ProblemEvidence"), mFont10)
.DrawString(drTP("ProblemEvidence"), mFont10, bBrush, New RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("PhaseGoal"), mFont10)
.DrawString(drTP("PhaseGoal"), mFont10, bBrush, New RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTReview"), mFont10)
.DrawString(drTP("FTReview"), mFont10, bBrush, New RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTStrength"), mFont10)
.DrawString(drTP("FTStrength"), mFont10, bBrush, New RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTRecommend"), mFont10)
.DrawString(drTP("FTRecommend"), mFont10, bBrush, New RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height

e.HasMorePages = True
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdTP.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try

End With

End Sub

#End Region
 
'Start on the next page
e.HasMorePages = True

If I'm not mistaken, setting the HasMorePages property to True just causes
the print engine to call the PrintPage event again. It doesn't make the
output go to a new page. You must keep track, externally, what page your
on so you can determine what data should be rendered on that page. You're
setting HasMorePages to True and then moving back to the top of the *same
page* and printing again.

Something like this (Pseudocode):

Private intPageNumber As Integer

Private Sub mDoc_PrintPage(...) Handles mDoc.PrintPage
With e.Graphics

'Get data to print for page intPageNumber here

'Code to print data for page intPageNumber here

If yPos > e.MarginBounds.Bottom Then
If bStillMoreDataToPrint Then
'Set the next page number to print
intPageNumber += 1
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Exit Sub
End If
End With
End Sub

Note that *after* setting HasMorePages, we don't print any more data. We
just exit the sub. If HasMorePages is True, then the PrintPage sub will be
called again.

I'm sure someone can explain it better than I can (and correct my
mistakes).
 
The print engine jumps to the next page when the PrintPage events is raised
again. HasMorePages have to be set true in the PrintPage Event's handler
because that instruct your PrintDocument to raise the event again after the
handler is left.
Because your HasMorePages is set true in the body of a do while loop after
you print the first page of your report the PrintPage Event's handler is not
left so the PrintPage Event is not raised again and the print engine doesn't
jump to the next page.


dls said:
Here is the code segment. I am printing from several datareaders. The
first page formats great. After the first page, the data dumps on the same
page, but continues to add pages. Also, the output from the database text
fields can be as much as 2000 char. Any advice on using measurestring
function would be appreciated.
Thanks for your help!

#Region "Print TreatmentPlan"

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPreview.Click
Dim dlg As New PrintPreviewDialog
dlg.Document = mDoc
dlg.WindowState = FormWindowState.Maximized
dlg.ShowDialog()
End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click
Dim dlgPrint As New PrintDialog
dlgPrint.Document = mDoc
If dlgPrint.ShowDialog = DialogResult.OK Then mDoc.Print()
End Sub

Private Sub mDoc_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles mDoc.PrintPage
Dim xPos As Single = e.MarginBounds.Left
Dim yPos As Single = e.MarginBounds.Top
Dim line8Heigth As Single = mFont8.GetHeight(e.Graphics)
Dim line10Heigth As Single = mFont10.GetHeight(e.Graphics)
Dim line12Heigth As Single = mFont12.GetHeight(e.Graphics)
Dim bBrush As Brush = Brushes.Black
Dim lineCount As Integer = 0
Dim sSCCLogo As String = sImagePath & "scc_seal.jpg"
Dim fi As New System.IO.FileInfo(sPhoto)

With e.Graphics
.DrawImage(Image.FromFile(sSCCLogo), xPos, yPos, 172, 122)
xPos = 425
yPos = e.MarginBounds.Top
.DrawString("DSHS SPECIAL COMMITTMENT CENTER", mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
xPos = 425
lineCount += 1
.DrawString("Resident Trimester Treatment Plan", mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
lineCount += 1
.DrawString("Trimester: " & sTrimester, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Date: " & Date.Today.ToShortDateString, mFont12, bBrush, xPos, yPos)

If fi.Exists Then
xPos = e.MarginBounds.Right - 120
.DrawImage(Image.FromFile(sPhoto), xPos, yPos, 120, 130)
Else
xPos = 500
yPos = 250
.DrawString("Resident Photo Unavailable", mFont10, bBrush, xPos, yPos)
End If

xPos = e.MarginBounds.Left
yPos = 250
.DrawString("Resident: " & sResident, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Committment Status: " & sCommitmentStatus, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Treatment Phase: " & iPhaseID, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Privilege Level: " & sPrivilegeLevel, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Plan Coordinator: " & sEvaluator, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Victim Profile: " & sVictimProfile, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Violence History: " & sViolenceHistory, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
yPos = 450
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line10Heigth
'yPos += line12Heigth
.DrawString("RESIDENT DIAGNOSES", mFont12, bBrush, xPos, yPos)
yPos += line10Heigth
yPos += line10Heigth
.DrawString("AXIS", mFont10, bBrush, xPos, yPos)
.DrawString("DSMCode", mFont10, bBrush, xPos + 50, yPos)
.DrawString("Diagnosis", mFont10, bBrush, xPos + 125, yPos)
.DrawString("Defined", mFont10, bBrush, xPos + 425, yPos)
.DrawString("Evaluator", mFont10, bBrush, xPos + 525, yPos)
yPos += line10Heigth

Dim cmdDX As New SqlCommand("sp_GetResidentDiagnoses", sqlCN)
With cmdDX
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
End With
Try
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drDX As SqlDataReader = cmdDX.ExecuteReader(CommandBehavior.CloseConnection)
Do While drDX.Read
.DrawString(drDX("Axis"), mFont10, bBrush, xPos, yPos)
.DrawString(drDX("DSMCode"), mFont10, bBrush, xPos + 50, yPos)
.DrawString(drDX("Diagnosis"), mFont10, bBrush, xPos + 125, yPos)
.DrawString(drDX("DateDefined"), mFont10, bBrush, xPos + 425, yPos)
.DrawString(drDX("ForensicEvaluator"), mFont10, bBrush, xPos + 525, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdDX.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try

yPos += line10Heigth
yPos += line10Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line10Heigth
yPos += line10Heigth
.DrawString("CLASS COMPLETIONS", mFont12, bBrush, xPos, yPos)
.DrawString("GROUP COMPLETIONS", mFont12, bBrush, 425, yPos)
yPos += line10Heigth
Dim yGroup As Single = yPos

Dim cmdCC As New SqlCommand("sp_GetResidentClassCompletions", sqlCN)
With cmdCC
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
End With

Try
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drCC As SqlDataReader = cmdCC.ExecuteReader(CommandBehavior.CloseConnection)
Do While drCC.Read
.DrawString(drCC("Opportunity"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdCC.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try
Dim yClassEndPos As Single = yPos
yPos = yGroup
Dim cmdGP As New SqlCommand("sp_GetResidentGroupCompletions", sqlCN)
With cmdGP
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
End With

xPos = 425
Try
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drGP As SqlDataReader = cmdGP.ExecuteReader(CommandBehavior.CloseConnection)
Do While drGP.Read
.DrawString(drGP("Opportunity"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdCC.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try

'set the position to the last written line
xPos = e.MarginBounds.Left
If yPos > yClassEndPos Then
yPos += line10Heigth
Else
yPos = yClassEndPos + line10Heigth
End If

'Start on the next page
e.HasMorePages = True

'Set the position to upper left of page
xPos = e.MarginBounds.Left
yPos = e.MarginBounds.Top

Dim cmdTP As New SqlCommand("sp_GetResidentTreamentPlan", sqlCN)
With cmdTP
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
.Parameters.Add("@Trimester", SqlDbType.Char, 6).Value = sTrimester
End With

Try
Dim sF As SizeF
Dim fmt As StringFormat
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drTP As SqlDataReader = cmdTP.ExecuteReader(CommandBehavior.CloseConnection)
Do While drTP.Read
'print page heading
.DrawString("DSHS SPECIAL COMMITTMENT CENTER", mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Resident Trimester Treatment Plan: " & sResident, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

'Print data
.DrawString("Domain Problem: " & drTP("DomainProblem"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
yPos += line10Heigth

.DrawString("Evidenced by behavior: ", mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
yPos += line10Heigth

sF = e.Graphics.MeasureString(drTP("ProblemEvidence"), mFont10)
.DrawString(drTP("ProblemEvidence"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("PhaseGoal"), mFont10)
.DrawString(drTP("PhaseGoal"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTReview"), mFont10)
.DrawString(drTP("FTReview"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTStrength"), mFont10)
.DrawString(drTP("FTStrength"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTRecommend"), mFont10)
.DrawString(drTP("FTRecommend"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
 
The print engine jumps to the next page when the PrintPage events is raised
again. HasMorePages have to be set true in the PrintPage Event's handler
because that instruct your PrintDocument to raise the event again after the
handler is left.
Because your HasMorePages is set true in the body of a do while loop after
you print the first page of your report the PrintPage Event's handler is not
left so the PrintPage Event is not raised again and the print engine doesn't
jump to the next page.
dls said:
Here is the code segment. I am printing from several datareaders. The
first page formats great. After the first page, the data dumps on the same
page, but continues to add pages. Also, the output from the database text
fields can be as much as 2000 char. Any advice on using measurestring
function would be appreciated.
Thanks for your help!

#Region "Print TreatmentPlan"

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPreview.Click
Dim dlg As New PrintPreviewDialog
dlg.Document = mDoc
dlg.WindowState = FormWindowState.Maximized
dlg.ShowDialog()
End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click
Dim dlgPrint As New PrintDialog
dlgPrint.Document = mDoc
If dlgPrint.ShowDialog = DialogResult.OK Then mDoc.Print()
End Sub

Private Sub mDoc_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles mDoc.PrintPage
Dim xPos As Single = e.MarginBounds.Left
Dim yPos As Single = e.MarginBounds.Top
Dim line8Heigth As Single = mFont8.GetHeight(e.Graphics)
Dim line10Heigth As Single = mFont10.GetHeight(e.Graphics)
Dim line12Heigth As Single = mFont12.GetHeight(e.Graphics)
Dim bBrush As Brush = Brushes.Black
Dim lineCount As Integer = 0
Dim sSCCLogo As String = sImagePath & "scc_seal.jpg"
Dim fi As New System.IO.FileInfo(sPhoto)

With e.Graphics
.DrawImage(Image.FromFile(sSCCLogo), xPos, yPos, 172, 122)
xPos = 425
yPos = e.MarginBounds.Top
.DrawString("DSHS SPECIAL COMMITTMENT CENTER", mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
xPos = 425
lineCount += 1
.DrawString("Resident Trimester Treatment Plan", mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
lineCount += 1
.DrawString("Trimester: " & sTrimester, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Date: " & Date.Today.ToShortDateString, mFont12, bBrush, xPos, yPos)

If fi.Exists Then
xPos = e.MarginBounds.Right - 120
.DrawImage(Image.FromFile(sPhoto), xPos, yPos, 120, 130)
Else
xPos = 500
yPos = 250
.DrawString("Resident Photo Unavailable", mFont10, bBrush, xPos, yPos)
End If

xPos = e.MarginBounds.Left
yPos = 250
.DrawString("Resident: " & sResident, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Committment Status: " & sCommitmentStatus, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Treatment Phase: " & iPhaseID, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Privilege Level: " & sPrivilegeLevel, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Plan Coordinator: " & sEvaluator, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Victim Profile: " & sVictimProfile, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Violence History: " & sViolenceHistory, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
yPos = 450
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line10Heigth
'yPos += line12Heigth
.DrawString("RESIDENT DIAGNOSES", mFont12, bBrush, xPos, yPos)
yPos += line10Heigth
yPos += line10Heigth
.DrawString("AXIS", mFont10, bBrush, xPos, yPos)
.DrawString("DSMCode", mFont10, bBrush, xPos + 50, yPos)
.DrawString("Diagnosis", mFont10, bBrush, xPos + 125, yPos)
.DrawString("Defined", mFont10, bBrush, xPos + 425, yPos)
.DrawString("Evaluator", mFont10, bBrush, xPos + 525, yPos)
yPos += line10Heigth

Dim cmdDX As New SqlCommand("sp_GetResidentDiagnoses", sqlCN)
With cmdDX
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
End With
Try
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drDX As SqlDataReader = cmdDX.ExecuteReader(CommandBehavior.CloseConnection)
Do While drDX.Read
.DrawString(drDX("Axis"), mFont10, bBrush, xPos, yPos)
.DrawString(drDX("DSMCode"), mFont10, bBrush, xPos + 50, yPos)
.DrawString(drDX("Diagnosis"), mFont10, bBrush, xPos + 125, yPos)
.DrawString(drDX("DateDefined"), mFont10, bBrush, xPos + 425, yPos)
.DrawString(drDX("ForensicEvaluator"), mFont10, bBrush, xPos + 525, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdDX.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try

yPos += line10Heigth
yPos += line10Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line10Heigth
yPos += line10Heigth
.DrawString("CLASS COMPLETIONS", mFont12, bBrush, xPos, yPos)
.DrawString("GROUP COMPLETIONS", mFont12, bBrush, 425, yPos)
yPos += line10Heigth
Dim yGroup As Single = yPos

Dim cmdCC As New SqlCommand("sp_GetResidentClassCompletions", sqlCN)
With cmdCC
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
End With

Try
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drCC As SqlDataReader = cmdCC.ExecuteReader(CommandBehavior.CloseConnection)
Do While drCC.Read
.DrawString(drCC("Opportunity"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdCC.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try
Dim yClassEndPos As Single = yPos
yPos = yGroup
Dim cmdGP As New SqlCommand("sp_GetResidentGroupCompletions", sqlCN)
With cmdGP
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
End With

xPos = 425
Try
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drGP As SqlDataReader = cmdGP.ExecuteReader(CommandBehavior.CloseConnection)
Do While drGP.Read
.DrawString(drGP("Opportunity"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
Loop
Catch ex As Exception
sbMsg.Text = ex.Message
Finally
cmdCC.Dispose()
If Not sqlCN.State = ConnectionState.Closed Then sqlCN.Close()
End Try

'set the position to the last written line
xPos = e.MarginBounds.Left
If yPos > yClassEndPos Then
yPos += line10Heigth
Else
yPos = yClassEndPos + line10Heigth
End If

'Start on the next page
e.HasMorePages = True

'Set the position to upper left of page
xPos = e.MarginBounds.Left
yPos = e.MarginBounds.Top

Dim cmdTP As New SqlCommand("sp_GetResidentTreamentPlan", sqlCN)
With cmdTP
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@SCCNumber", SqlDbType.Char, 6).Value = sSCCNumber
.Parameters.Add("@Trimester", SqlDbType.Char, 6).Value = sTrimester
End With

Try
Dim sF As SizeF
Dim fmt As StringFormat
If Not sqlCN.State = ConnectionState.Open Then sqlCN.Open()
Dim drTP As SqlDataReader = cmdTP.ExecuteReader(CommandBehavior.CloseConnection)
Do While drTP.Read
'print page heading
.DrawString("DSHS SPECIAL COMMITTMENT CENTER", mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
.DrawString("Resident Trimester Treatment Plan: " & sResident, mFont12, bBrush, xPos, yPos)
yPos += line12Heigth
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

'Print data
.DrawString("Domain Problem: " & drTP("DomainProblem"), mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
yPos += line10Heigth

.DrawString("Evidenced by behavior: ", mFont10, bBrush, xPos, yPos)
yPos += line10Heigth
yPos += line10Heigth

sF = e.Graphics.MeasureString(drTP("ProblemEvidence"), mFont10)
.DrawString(drTP("ProblemEvidence"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("PhaseGoal"), mFont10)
.DrawString(drTP("PhaseGoal"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTReview"), mFont10)
.DrawString(drTP("FTReview"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTStrength"), mFont10)
.DrawString(drTP("FTStrength"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
yPos += line10Heigth + sF.Height
yPos += line12Heigth
.DrawLine(Pens.Black, e.MarginBounds.Left, yPos, e.MarginBounds.Right, yPos)
yPos += line12Heigth

sF = e.Graphics.MeasureString(drTP("FTRecommend"), mFont10)
.DrawString(drTP("FTRecommend"), mFont10, bBrush, New
RectangleF(e.MarginBounds.Left, yPos, sF.Width, sF.Height), fmt)
 
Back
Top