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