.NET RichTextBox:无法更改 Rtf 属性
也许我在这里遗漏了一些真正简单的东西,但我一直在努力更改 RichTextBox 的 RTF 属性,以便对我的文本应用一些颜色编码。我遇到的问题最直接的例子可能是设置 Rtf 属性以在其标头中包含颜色表。
Rtf 属性返回的默认 RTF 字符串:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17\par}
以及我想使用颜色表设置的新 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}
我设置它使用:
RichTextBox richTextBox = new RichTextBox();
richTextBox.Rtf = rtfStr; // My new RTF string, as seen above.
但是,通过调试器,可以观察到 Rtf 属性顽固地拒绝更改;没有抛出异常,它只是拒绝改变。当我 string.Replace() 单词以在其周围包含 RTF 颜色标签时,也会发生同样的问题。我还尝试关闭文本框上的任何只读属性。
任何建议都会非常有帮助,谢谢!
- 戴夫
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用内置功能来改变颜色?
或者,如果您确实需要弄乱 RTF 格式,请以编程方式设置颜色,然后查看它生成的 RTF,并尝试一下。也许格式不正确,所以它会默默地消除错误。
编辑:
另外,我希望您实际上并不是每次都创建一个新的 RTB。如果是,从您的示例来看,您没有将其添加到控件集合中,在这种情况下,无论如何都不会看到它。
Why not use the built in functionality to change color?
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.
正如 Jeremy 提到的,在分配给 .Rtf 属性后,.NET 中的 RichTextBox 将自动重新格式化您的 RTF 数据,以简化和标准化它。当您添加颜色表时,并不是 RichTextBox 拒绝更改,而是您实际上没有使用任何这些颜色,因此它们被简化了。只要添加一些彩色文本来使用每个新的颜色代码,RichTextBox 就会保留您的自定义颜色表。
因此,如果您不想使用 Jeremy 提到的简单属性,则需要跟踪已添加到颜色表中的颜色以及它们的索引是什么。如果该控件可由用户编辑,您还需要能够解析当前颜色表,因为用户可以删除给定颜色的所有文本并导致从颜色表中删除颜色(可能导致颜色索引重新编号)。
这是 CodeProject 上的一篇文章,涵盖了一些基础知识,但没有将颜色表添加到正确的位置,也没有涉及重新解析颜色表: http://www.codeproject.com/KB/cs/RTFSyntaxColour.aspx
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