简体   繁体   中英

.NET RichTextBox: unable to change Rtf property

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

Why not use the built in functionality to change color?

    rtbPreview.SelectionStart = 1;
    rtbPreview.SelectionLength = 3;
    rtbPreview.SelectionFont = newFont;
    rtbPreview.SelectionColor = Color.Red;

Or, if you really need to mess with the RTF format, set the color programmatically, then see what RTF it generates, and give that a try. Maybe the format is not correct so it is silently squelching an error.

Edit: Also, I hope you are not actually creating a new RTB every time. If you are, it looks from your sample that you're not adding it to the controls collection in which case it will never be seen anyways.

As Jeremy mentioned, the RichTextBox in .NET will automatically reformat your RTF data to simplify and standardize it after you assign to the .Rtf property. When you add your color table, it's not that the RichTextBox refuses to change, but rather that you aren't actually using any of those colors, so they get simplified back out. So long as you add some colored text to use each new color code, the RichTextBox will keep your custom colortable.

Consequently, if you don't want to use the simple properties that Jeremy mentioned, you will need to keep track of which colors you have already added to the color table and what their indexes are. If the control is editable by the user, you will furthermore need the ability to parse out the current color table, as the user could delete all of the text in a given color and cause the removal of a color from the color table, (likely causing color index renumbering).

Here's an article on CodeProject that covers some of the basics, but doesn't add the color table to quite the right place, nor does it deal with reparsing the color table: http://www.codeproject.com/KB/cs/RTFSyntaxColour.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM