Results 1 to 3 of 3

Thread: [RESOLVED] Print Rich Text Box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Resolved [RESOLVED] Print Rich Text Box

    Hello

    Using the following code to print the contents of my rich textbox

    Code:
    Private Sub PrintText(ByVal sender As Object, _
                               ByVal ev As PrintPageEventArgs)
    
            tbUpperBody.Text = vbNewLine &
                                vbNewLine &
                                tbUpperBody.Text
    
            Dim blackPen As New Pen(Color.Black, 3)
            ' Create points that define line.
            Dim point1 As New Point(75, 150)
            Dim point2 As New Point(725, 150)
            ' Draw line to screen.
            ev.Graphics.DrawLine(blackPen, point1, point2)
            ev.Graphics.DrawString(tbUpperBody.Text, New Font("Arial", _
                                                                14, FontStyle.Regular), Brushes.Black, 120, 120)
    
         
    
            ev.HasMorePages = False
    
        End Sub
    
        Private Sub PrintTopToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintTopToolStripMenuItem.Click
    
            Try
                Dim PrintDoc As New PrintDocument
                AddHandler PrintDoc.PrintPage, AddressOf Me.PrintText
                PrintDoc.Print()
                tbUpperBody.Multiline = True
            Catch ex As Exception
                MessageBox.Show("Sorry - there was a problem with printing", ex.ToString())
            End Try
        End Sub
    Issue is that when the rich textbox contains text that is spread over multiple lines, it prints as one massive line, which obviously disappears off the edge of the page.

    how can i get it to print with line breaks?

    thanks

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Print Rich Text Box

    The Text property of a TextBox or RichTextBox just returns a string of characters -- no formatting, line endings etc. But you can print a string with word wrapping by using the version of Graphics.DrawString with a Rectangle argument, for example:
    Code:
    Dim rect As New Rectangle(120, 120, 250, 250)
    ev.Graphics.DrawString(tbUpperBody.Text, New Font("Arial", _
                                         14, FontStyle.Regular), Brushes.Black, rect)
    It's up to you to decide the position and size of the rectangle.

    BB

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: Print Rich Text Box

    Awesome!

    Thank you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width