RichTextBox Details

Steve Andrews 0 Reputation points
2024-05-23T21:05:20.31+00:00

I need more information about the RichTextBox in winforms/dotnet 8/C# 12/VS2022.

Specifically, when I create custom RTF content based on the v1.5 spec and try to insert it (as selection or entire document), the RichTextBox strips out all of my custom RTF. This includes custom custom styleheets (stylesheet), font tables (fonttbl), color tables (colortbl), annotations (v ... annotation), etc.

  1. What version of the RTF spec does the RichTextBox in dotnet 8 / C# 12 / VS2022 support?
  2. What header elements and control words does the RichTextBox support?
  3. If 1 and 2 get me nowhere, which version of the richedit control is the RichTextBox using and does it support these features? Can I P/Invoke the RTF functionality I need?

My primary issues are that the RichTextBox is not accepting all RTF content. For example:

When I try to set the Rtf property on a brand new RichTextBox to: {\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil Calibri;}} {\colortbl ;\red246\green246\blue246;\red0\green0\blue0;} {\stylesheet {\fs11 \sbasedon222\snext0 Normal;} {\s1\sbasedon0\snext0\fs25.6\b\cf1 Heading 1;} {\s2\sbasedon0\snext0\fs22.4\b\cf1 Heading 2;} {\s3\sbasedon0\snext0\fs21.0\cf1 Heading 3;} {\s4\sbasedon0\snext0\fs24\qc\li720\ri720\cf1 Quote;} } {\*\generator CustomGenerator 0.45}\viewkind4\uc1 \par \pard\plain\sa200\sl259\slmult1\f0\fs22\cf1 \par }

The resulting Rtf has been changed to: {\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil Calibri;}} {\colortbl ;\red246\green246\blue246;} {\*\generator Riched20 10.0.19041}\viewkind4\uc1 \pard\f0\fs24\lang1033\par \pard\sa200\sl252\slmult1\cf1\fs22\par }

And when I try to add an annotation: {\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil Calibri;}} {\colortbl ;\red246\green246\blue246;} {\*\generator Riched20 10.0.19041}\viewkind4\uc1 \pard\f0\fs24\lang1033\par \pard\sa200\sl252\slmult1\cf1\fs22\par } {\v\fs16 {\atnid AU}\chatn{\*\annotation \pard\plain \s224 \fs20 {\field{\fldinst page \\#'"Page: '#'\line'"}{\fldrslt}}{\fs16 \chatn } This is my annotation}}}

This is the subsequent resulting RTF: {\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil Calibri;}} {\colortbl ;\red246\green246\blue246;} {\*\generator Riched20 10.0.19041}\viewkind4\uc1 \pard\f0\fs24\lang1033\par \pard\sa200\sl252\slmult1\cf1\fs22\par \pard\cf0\fs24\par \pard\sa200\sl252\slmult1\cf1\fs22\par }

Does the RichTextBox not support these things? Am I doing it wrong?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,923 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 46,466 Reputation points Microsoft Vendor
    2024-05-24T03:23:26.1966667+00:00

    Hi @Steve Andrews , Welcome to Microsoft Q&A,

    The RichTextBox control is based on the Windows RichEdit control, and different versions of the RichEdit control support different versions of the RTF specification. Specifically:

    • RichEdit 1.0: Support RTF 1.0 specification.
    • RichEdit 2.0: Support RTF 1.2 specification.
    • RichEdit 3.0: Support RTF 1.5 specification.
    • RichEdit 4.1 and higher: Supports RTF 1.8 and higher (including parts of the Office Open XML specification).

    In .NET 6 and later (including .NET 8), the RichTextBox control defaults to RichEdit 4.1, so it supports most features in the RTF 1.8 specification.

    The RTF control words and elements supported by the RichTextBox control are determined by the underlying RichEdit control. Generally speaking, it supports most common RTF control words, but for some advanced or custom control words, it may ignore or strip them. This includes:

    • Font table (fonttbl): Supported, but unknown fonts will be replaced based on system fonts.
    • Color table (colortbl): Supported.
    • Stylesheet: Limited support, custom styles may be ignored.
    • Paragraph format (par, pard, qj, etc.): Supported.
    • Character format (b, i, ul, etc.): Supported.
    • Picture: Supported.
    • Comments (*\v): Usually ignored.

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.