简体   繁体   English

.NET RichTextBox:无法更改Rtf属性

[英].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. 也许我在这里错过了一些真正简单的事情,但是我一直在努力更改RichTextBox的RTF属性,以便对文本应用一些颜色编码。 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. 我遇到的问题的最直接的例子可能是将Rtf属性设置为在其标题中包含颜色表。

The default RTF string returned by the Rtf property: Rtf属性返回的默认RTF字符串:

{\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: 我想在颜色表中设置新的RTF字符串:

{\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; 但是,通过调试器,可以观察到Rtf属性顽固地拒绝更改。 no exceptions are thrown, it just refuses to change. 没有抛出异常,它只是拒绝更改。 Same issue happens when I string.Replace() words to include RTF color tags around them. 当我使用string.Replace()单词在它们周围包含RTF颜色标签时,也会发生相同的问题。 I've also tried turning off any ReadOnly properties on the text box. 我也尝试过关闭文本框中的所有ReadOnly属性。

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. 或者,如果您真的需要弄乱RTF格式,请以编程方式设置颜色,然后查看生成的RTF,然后尝试一下。 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. 编辑:另外,我希望您实际上并不是每次都创建一个新的RTB。 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. 正如Jeremy所述,.NET中的RichTextBox将在您分配给.Rtf属性后自动重新格式化RTF数据,以简化和标准化数据。 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. 当您添加颜色表时,并不是RichTextBox拒绝更改,而是您实际上并没有使用任何这些颜色,因此可以简化它们。 So long as you add some colored text to use each new color code, the RichTextBox will keep your custom colortable. 只要您添加一些彩色文本以使用每个新的颜色代码,RichTextBox就会保留您的自定义颜色表。

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. 因此,如果您不想使用Jeremy提到的简单属性,则需要跟踪已添加到颜色表中的颜色及其索引。 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 这是有关CodeProject的文章,涵盖了一些基础知识,但没有将颜色表添加到正确的位置,也没有涉及重新解析颜色表: http : //www.codeproject.com/KB/cs/ RTFSyntaxColour.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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