RichTextBox.GetPreferredSize seems broken because RTF \line tag handling changed between .net framework 4.8 and .net 8

Withey, Rob 20 Reputation points
2023-12-04T12:56:25.1466667+00:00

When generating the Text property of a RichTextBox from RTF containing a \line tag, the output differs between .net framework 4.8 and .net 8.

For RTF input of : {\rtf1\ansi line1\line line2}

  • .Net framework 4.8 generates Text of : line1\nline2
  • .Net 8 generates Text of : line1\vline2

The difference (\n -> \v) means that when RichTextBox.GetPreferredSize is called with the Text a different size is returned. .net 8 appears to treat Text (with a \v) as a single line, whereas .net framework 4.8 treats Text (with a \n) as two lines.

A workaround is to use \par instead of \line, but I'd like to understand why the behaviour has changed.

Some code to demonstrate this is in the attached Form1.txt. Form1.txt In each case the upper rich text box uses \line, the lower rich text box uses \par.

When built and run under .net framework 4.8 this produces:

net48_result

And under .net 8 this produces:

net8_result

Why did the behaviour change, and is there a way to prevent the difference (without changing the RTF to use \par)?

Thanks,

Rob

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,643 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2023-12-05T07:00:13.2466667+00:00

    Hi @Withey, Rob , Welcome to Microsoft Q&A,

    This difference is not unique to .net 8.

    You can test it, it will recognize /line as a line in .net.

    It does not belong to the same platform as .Net Framework.

    In principle:

    \line: This command is used to insert a newline character, indicating that a new line starts at the current position in the text. But it is worth noting that \line does not end the previous paragraph, but only breaks the line within the current paragraph.

    \par: This command indicates the beginning of a new paragraph. When using \par, a new paragraph will be started, and the text in the new paragraph will start from the beginning of the paragraph. \par will insert a newline character at the current position and start a new paragraph.

    On the contrary, it is more logical in .net.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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 additional answers

Sort by: Most helpful