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

ВЕСЕЛИН ДИМОВ 41 Reputation points
2021-11-17T14:14:29.853+00:00

Hello.
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
        PrintPreviewDialog1.ShowDialog()

    End Sub*

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


But resulu is:
printpreview_02.png

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

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.
722 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 26,271 Reputation points Microsoft Vendor
    2021-11-18T03:29:27.88+00:00

    Hi @50683435 ,
    You can define the StringFormat to wrap automatically, and then make the text drawing area a rectangle.
    Here is my result.
    150430-2021-11-18-111333.png
    150462-2021-11-18-111712.png
    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.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Castorix31 81,461 Reputation points
    2021-11-17T17:53:40.297+00:00

    This works for me with a RichTextBox1 control =>

    150325-printpreviewdialog.jpg

    For int As Integer = 0 To 10  
        RichTextBox1.AppendText("This is a test" + Environment.NewLine)  
    Next  
      
    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  
    previewdlg.ShowDialog()  
    

    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  
    
    0 comments No comments

  2. ВЕСЕЛИН ДИМОВ 41 Reputation points
    2021-11-18T08:08:10.27+00:00

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

    0 comments No comments

  3. ВЕСЕЛИН ДИМОВ 41 Reputation points
    2021-11-18T17:22:05.993+00:00

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