.NET RichTextBox: unable to change Rtf property.

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2010-06-10T19:29:35Z Indexed on 2010/06/10 19:32 UTC
Read the original article Hit count: 345

Filed under:
|
|
|

Hey guys,

Perhaps I'm missing something real simple here, but I've been struggling to change the RTF property of my RichTextBox in order to apply some color coding to my text. Probably the most straight-forward example of the problem I'm having is setting the Rtf property to include a color table in its header.

The default RTF string returned by the Rtf property:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17\par}

And the new RTF string I'd like to set with my color table:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}{\colortbl;\red128\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;}}\viewkind4\uc1\pard\f0\fs17\par}

And I set this using:

RichTextBox richTextBox = new RichTextBox();
richTextBox.Rtf = rtfStr; // My new RTF string, as seen above.

However, via debugger, it can be observed the that Rtf property stubbornly refuses to change; no exceptions are thrown, it just refuses to change. Same issue happens when I string.Replace() words to include RTF color tags around them. I've also tried turning off any ReadOnly properties on the text box.

Any suggestions would be most helpful, thanks!

  • Dave

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET