0

I am trying to add a list of strings that contain special characters such as accents to a RichEditText box. To be exact, it is list of peoples names. Some are in French & Spanish for instance. Unfortunately, these special characters are showing up incorrectly.

I am currently do the following:

aRichEditTextBox.Lines = nameList.ToArray();

For instance... Instead of "Añazgo", I am getting this "A�azgo".

What is the proper or best way to fix this issue?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Are you using WinForms or WPF? – Jackdaw Mar 15 '21 at 18:16
  • Can you check the nameList content before assigning it to RichEditText box and be sure that it has "Añazgo"? – Ali Ihsan Elmas Mar 15 '21 at 18:27
  • Where is the `nameList` content coming from? – Jimi Mar 15 '21 at 18:29
  • @Jackdraw, I am using Windows Forms – Samario Mar 16 '21 at 22:22
  • @Jimi, it is a list where the data comes from a text file. – Samario Mar 16 '21 at 22:23
  • Yes, sure, so the source is a text file. Which is the encoding used to write to this file? `Encoding.Default`? Can you post a sample of this file? - Or, open the file in Notepad, see whether you can read it correctly, then save it using UTF-8 as the Encoding – Jimi Mar 16 '21 at 22:24
  • @AliIhsanElmas, I know what the data looks like because it is being read from a text file into a List. When looking at the list items in the debugger, I am seeing the accents in the names of the individuals listed in the List. – Samario Mar 16 '21 at 22:25
  • @Jimi... here is a sample of what is in the text file. Añazgo, Carlos Deza, Silvia Garcia, Tomás Reyes, José Suárez, María – Samario Mar 16 '21 at 22:43
  • That's not what I asked and suggested. Open the file in Notepad and save it as UTF-8. Then try again. Remember to use a Font that contains those characters. – Jimi Mar 16 '21 at 22:44
  • @Jimi, yes, saving it as UTF-8 seemed to have worked. – Samario Mar 16 '21 at 22:48
  • Right. From now on, only use UTF-8 to store text. If you don't, you can read it in your Computer, most probably, others cannot read it correctly (or at all) in other machines that use a different Language and CodePages. Building a software, you cannot consider the Local Encoding as a valid encoding for text. UTF-8 can render Unicode CodePoints of any Language and is interpreted everywhere. – Jimi Mar 16 '21 at 22:53
  • @Jimi, thanks for the help! The text file is not one created by me and is being provided by someone else. This means I will have to add code to convert the text file first to UTF-8 before adding the names to my sting list. – Samario Mar 16 '21 at 22:59
  • Ah, well, sure. Then, read the source text specifying the Encoding used (it should be CodePage 1252), then save it all with `File.WriteAllText([Path], [Text])`. The default Encoding in .Net is UTF-8, so you don't need to specify the encoding when saving text. – Jimi Mar 16 '21 at 23:02

1 Answers1

0

you have to use an RTF format for the richtextbox to recognize those special character, use this method that came from here - RichTextBox and special chars c#

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();
}

richtextbox1.Rtf = GetRtfUnicodeEscapedString(TextString);
psy
  • 48
  • 5