I have some C#.NET code to take a string contain开发者_Python百科ing custom color codes and use them show colorized text in a RichTextBox. It works great on Windows, but running under Linux on Mono 2.4.2.3 I get this error:
RTF Parsing failure: Malformed color entry
RTF Stream Info: Pos:324 Line:1
TokenClass:Text, Major:8, Minor:0, Param:-10000000, Text:
Here is the code:
/// <summary>
/// Populates a RichTextBox with text generated from color code strings.
/// </summary>
/// <param name="text"></param>
/// <param name="target"></param>
public static void BuildRTFString(string text, RichTextBox target)
{
// Add header and build color table.
const string rtfHeader = "{\\rtf\\ansi{\\colortbl\\red192\\green192\\blue192;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;}\\cf0 ";
// Replace each color one by one.
string parsedText = text.Replace("&+l", "\\cf1 ");
parsedText = parsedText.Replace("&+B", "\\cf2 ");
parsedText = parsedText.Replace("&+C", "\\cf3 ");
parsedText = parsedText.Replace("&+G", "\\cf4 ");
parsedText = parsedText.Replace("&+M", "\\cf5 ");
parsedText = parsedText.Replace("&+R", "\\cf6 ");
parsedText = parsedText.Replace("&+Y", "\\cf7 ");
parsedText = parsedText.Replace("&+W", "\\cf8 ");
parsedText = parsedText.Replace("&+b", "\\cf9 ");
parsedText = parsedText.Replace("&+c", "\\cf10 ");
parsedText = parsedText.Replace("&+g", "\\cf11 ");
parsedText = parsedText.Replace("&+m", "\\cf12 ");
parsedText = parsedText.Replace("&+r", "\\cf13 ");
parsedText = parsedText.Replace("&+y", "\\cf14 ");
parsedText = parsedText.Replace("&+L", "\\cf15 ");
parsedText = parsedText.Replace("&+w", "\\cf16 ");
parsedText = parsedText.Replace("&n", "\\cf16 ");
parsedText = parsedText.Replace("&N", "\\cf16 ");
target.Rtf = rtfHeader + parsedText + "}";
}
This error happens no matter what text is supplied to the method and causes the RichTextBox to render empty.
Is there anything I can do to make this work, or is the RichTextBox under Mono just not quite done yet (or too different to use the way I intend)?
If you'd like to see an example of how it is used, check this screenshot: http://www.basternae.org/images/RtfTextBox.png
精彩评论