Printing the contents of a multiline-textbox with pagebreaks 
Author Message
 Printing the contents of a multiline-textbox with pagebreaks

Hi,

Iam a newbie to vb.net and I was missing a simple code to print out a
multiline-textbox. I used the stream-print-example from MS and added
some code to print a textbox line by line.

I just wanna share this code with you. If you got a better way to
print textboxes then let me know.

Heres the code:

*** CODE START ***

    Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal
ev As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument.PrintPage

        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = ev.MarginBounds.Left
        Dim topMargin As Single = ev.MarginBounds.Top
        Dim Line As String = Nothing

        ' Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
printfont.GetHeight(ev.Graphics)

        ' Print each line of the file.

        While count < Math.Round(linesPerPage, 0) And (bigcount <
txt_main.Lines.Length) # it is better to round the lines per-page,
bigcount is the main-line-counter
            Line = txt_main.Lines.GetValue(bigcount) # get the next
line from the multiline-textbox

            yPos = topMargin + count *
printfont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(Line, printfont, Brushes.Black,
leftmargin, yPos, New StringFormat())
            count += 1
            bigcount += 1
        End While

        ' add a pagebreak until the bigcounter reaches the upper limit
of the textbox-line-count
        If bigcount < txt_main.Lines.Length Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If

*** END OF CODE ***

bye

Robby - Hamburg



Fri, 11 Nov 2005 22:13:50 GMT  
 Printing the contents of a multiline-textbox with pagebreaks
Thank you, this was helpful in my being able to do the same thing.

There was a problem with your code though. If a line was wider than the
print space on the page it would get cut off.

I've modified the code to take long lines into account. I thought you might
be interested in the modified version. It's not totally dynamic, but it is
fine for my purposes. I set the margins to 1/4 inch all around the page,
then hard-coded the remaining page size in my calculations. It would be easy
enough to make the calculations more dynamic, but I didn't really need them
to be.

I should mention that this code has not been compiler checked. I took your
code and converted it to C# for my project. When I was done making the
changes I required, I ran it through a C# to VB.NET converter.

Dim linesPerPage As single =  0
Dim count As Integer =  0
Dim bigCount As Integer =  0
Dim leftMargin As Integer =  e.MarginBounds.Left
Dim yPos As single =  e.MarginBounds.Top
Dim line As String =  Nothing
Dim printFont As Font =  New Font("Arial",10)
Dim fontHeight As single =  0

fontHeight = printFont.GetHeight(e.Graphics)

linesPerPage = e.MarginBounds.Height / fontHeight

While count < Math.Round(linesPerPage,0) And bigCount <
txtResults.Lines.Length
    line = txtResults.Lines(bigCount)

    Dim rectHeight As single =
CType((Math.Ceiling(e.Graphics.MeasureString(line,printFont).Width / 800) *
fontHeight), single)

    e.Graphics.DrawString(line, printFont, Brushes.Black, New
RectangleF(leftMargin, yPos, 800, rectHeight), New StringFormat())

    yPos += rectHeight
    count = count + 1
    bigCount = bigCount + 1
End While

If bigCount < txtResults.Lines.Length Then
    e.HasMorePages = True
Else
    e.HasMorePages = False
End If

'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel (http://www.KamalPatel.net)
'----------------------------------------------------------------


Quote:
> Hi,

> Iam a newbie to vb.net and I was missing a simple code to print out a
> multiline-textbox. I used the stream-print-example from MS and added
> some code to print a textbox line by line.

> I just wanna share this code with you. If you got a better way to
> print textboxes then let me know.

> Heres the code:

> *** CODE START ***

>     Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal
> ev As System.Drawing.Printing.PrintPageEventArgs) Handles
> PrintDocument.PrintPage

>         Dim linesPerPage As Single = 0
>         Dim yPos As Single = 0
>         Dim count As Integer = 0
>         Dim leftMargin As Single = ev.MarginBounds.Left
>         Dim topMargin As Single = ev.MarginBounds.Top
>         Dim Line As String = Nothing

>         ' Calculate the number of lines per page.
>         linesPerPage = ev.MarginBounds.Height /
> printfont.GetHeight(ev.Graphics)

>         ' Print each line of the file.

>         While count < Math.Round(linesPerPage, 0) And (bigcount <
> txt_main.Lines.Length) # it is better to round the lines per-page,
> bigcount is the main-line-counter
>             Line = txt_main.Lines.GetValue(bigcount) # get the next
> line from the multiline-textbox

>             yPos = topMargin + count *
> printfont.GetHeight(ev.Graphics)
>             ev.Graphics.DrawString(Line, printfont, Brushes.Black,
> leftmargin, yPos, New StringFormat())
>             count += 1
>             bigcount += 1
>         End While

>         ' add a pagebreak until the bigcounter reaches the upper limit
> of the textbox-line-count
>         If bigcount < txt_main.Lines.Length Then
>             ev.HasMorePages = True
>         Else
>             ev.HasMorePages = False
>         End If

> *** END OF CODE ***

> bye

> Robby - Hamburg



Sun, 13 Nov 2005 09:37:10 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How do you print the content of a textbox with multiline=true and scrollbar active

2. Multiline Textbox Cause Page Overflows when Printing or doesn't print all lines

3. Print: how do I print the contents of a textbox

4. Newbie trying to print a multiline textbox

5. Printing Multiline Textbox Help Needed.

6. Printing multiline text from a textbox

7. Printing multiline textbox

8. printing a multiline textbox

9. Printing from multiline textboxes?????

10. Printing Text in Multiline TextBox

11. VB5: Want to print text in multiline TextBox

12. How I print multilines textbox?

 

 
Powered by phpBB® Forum Software