RichTextBox and C # special characters

I need to put RTF formatted text in a richtextbox, I am trying to put it with a parameter richtextbox.rtf = TextString

, but the problem is that the string has special characters and the richtextbox does not display the whole string correctly. The line and code I'm using:

String (TextString):

╔═══This is only an example, the special characters may change═══╗

C # code:

String TextString = System.Text.Encoding.UTF8.GetString(TextBytes);
String TextRTF = @"{\rtf1\ansi " + TextString + "}";
richtextbox1.Rtf = TextRTF;

      

With this code richtextbox show "+ --- This is just an example, special characters can change --- +", and in some cases show "??????".

How can I solve this problem? if i change \rtf1\ansi

to \rtf1\utf-8

i don't see the change.

+3


source to share


1 answer


You can simply use the property Text

:

richTextBox1.Text = "╔═══This is only an example, the special characters may change═══╗";

      

If you want to use the property RTF

: Take a look at this question: How to output a Unicode string to RTF (using C #)

You need to use something like this to convert special characters to rtf format:



static string GetRtfUnicodeEscapedString(string s)
{
    var sb = new StringBuilder();
    foreach (var c in s)
    {
        if(c == '\\' || c == '{' || c == '}')
            sb.Append(@"\" + c);
        else if (c <= 0x7f)
            sb.Append(c);
        else
            sb.Append("\\u" + Convert.ToUInt32(c) + "?");
    }
    return sb.ToString();
}

      

Then use:

richtextbox1.Rtf = GetRtfUnicodeEscapedString(TextString);

      

+3


source







All Articles