简体   繁体   English

将包含带重音符号的单词的字符串列表添加到 RichEditText 框的正确方法

[英]Proper way to add a list of strings that include words with accents to a RichEditText box

I am trying to add a list of strings that contain special characters such as accents to a RichEditText box.我正在尝试将包含特殊字符(例如重音符号)的字符串列表添加到 RichEditText 框中。 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".例如......而不是“Añazgo”,我得到这个“A�azgo”。

What is the proper or best way to fix this issue?解决此问题的正确或最佳方法是什么?

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#你必须使用 RTF 格式来识别那些特殊字符,使用来自这里的方法 - RichTextBox 和特殊字符 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);

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

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