How do I make the result of RichTextBox to be multilines?

ВЕСЕЛИН ДИМОВ 21 Reputation points

I need your help.
Visual Studio Community 2019, Visual Basic, for Windows

I have RichTextBox and button "PrintPreview". My code for PrintPreview is:

*Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        PrintPreviewDialog1.StartPosition = FormStartPosition.CenterParent
        PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.Icon = Icon
        PrintPreviewDialog1.Document = PrintDocument1

    End Sub*

This code work for me. But result is oneline.
I type this:

But resulu is:

In Properties of RichTextBox WordWrap is "True". How to make result to be multiline ?

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,830 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
433 questions
No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 10,911 Reputation points Microsoft Employee

    Hi @50683435 ,
    You can define the StringFormat to wrap automatically, and then make the text drawing area a rectangle.
    Here is my result.
    Here is my code which you can refer to.

        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage  
            Dim text As String = RichTextBox1.Text  
            Dim fmt As New StringFormat  
            fmt.LineAlignment = StringAlignment.Near  
            fmt.FormatFlags = StringFormatFlags.LineLimit  
            Dim size As New Size(PrintDocument1.DefaultPageSettings.PaperSize.Width, PrintDocument1.DefaultPageSettings.PaperSize.Height)  
            Dim r As New Rectangle(New Point(10, 10), size)  
            Dim printFont As New Font("Arial", 10, FontStyle.Regular)  
            e.Graphics.DrawString(text, printFont, Brushes.Black, r, fmt)  
        End Sub  

    Hope the code above could be helpful.
    Best Regards.
    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

3 additional answers

Sort by: Most helpful
  1. Castorix31 65,711 Reputation points

    This works for me with a RichTextBox1 control =>


    For int As Integer = 0 To 10  
        RichTextBox1.AppendText("This is a test" + Environment.NewLine)  
    Dim printdoc1 As PrintDocument = New PrintDocument  
    AddHandler printdoc1.PrintPage, AddressOf Me.printdoc1_PrintPage  
    Dim previewdlg As PrintPreviewDialog = New PrintPreviewDialog  
    previewdlg.PrintPreviewControl.Zoom = 1.5  
    previewdlg.Document = printdoc1  

    with :

    Private Sub printdoc1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)  
        Using bmp As New Bitmap(Me.Width, Me.Height)  
            RichTextBox1.DrawToBitmap(bmp, New Rectangle(0, 0, RichTextBox1.Width, RichTextBox1.Height))  
            Dim pageaBounds As Rectangle = e.PageBounds  
            e.Graphics.DrawImage(bmp, 10, 10)  
        End Using  
    End Sub  
    No comments

  2. ВЕСЕЛИН ДИМОВ 21 Reputation points

    Thank you friends. I will try and write a little later.

    No comments

  3. ВЕСЕЛИН ДИМОВ 21 Reputation points

    @Jiachen Li-MSFT your code is the answer. Thank you.
    Castorix31, thank you too.