How to print text in multiline textbox ?

F

fniles

I am using VB.NET 2008.
I have a multiline textbox on the form that I would like to print when the
user click on the "Print" button.
With my codes below, when the text are in more than 1 page (say 3 pages) of
the textbox, when it prints the text in the text box, the 2nd and 3rd pages
are printed on the same page with the 1st page.
So, instead of printing 3 pages, it prints 1 page with the text from page 2
and 3 are printed on top of each other.
Please let me know how I can do it correctly.
Thank you.

Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPrint.Click
prnDoc.DefaultPageSettings.Landscape = True
Dim strText As String = txtLog.Text
myReader = New StringReader(strText)
prnDoc.Print()
End Sub

Private Sub prnDoc_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles prnDoc.PrintPage
Dim linesPerPage As Single = 0
Dim yPosition As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim line As String = Nothing
Dim printFont As Font = txtLog.Font
Dim myBrush As New SolidBrush(Color.Black)

linesPerPage = e.MarginBounds.Height /
printFont.GetHeight(e.Graphics)
While count < linesPerPage
line = myReader.ReadLine()
If (line Is Nothing) Then
Exit While
Else
yPosition = topMargin + count *
printFont.GetHeight(e.Graphics)
e.Graphics.DrawString(line, printFont, myBrush, leftMargin,
yPosition, New StringFormat())
count += 1
If count >= linesPerPage Then
e.HasMorePages = True
count = 0
End If
End If
End While
e.HasMorePages = False
myBrush.Dispose()
 
F

fniles

Never mind.
I got my answer (thanks to a posting by Andy on www.vbforums.com).
This is how my code look like now:

Private Sub prnDoc_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles prnDoc.PrintPage
Dim linesPerPage As Single = 0
Dim yPosition As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim line As String = Nothing
Dim printFont As Font = txtLog.Font
Dim myBrush As New SolidBrush(Color.Black)
linesPerPage = e.MarginBounds.Height /
printFont.GetHeight(e.Graphics)
While count < linesPerPage
line = myReader.ReadLine()
If (line Is Nothing) Then
Exit While
Else
yPosition = topMargin + count *
printFont.GetHeight(e.Graphics)
e.Graphics.DrawString(line, printFont, myBrush, leftMargin,
yPosition, New StringFormat())
count += 1
End If
End While
If Not (line Is Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
myBrush.Dispose()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top