RichTextBox不打印换行符

本文关键字:换行符 打印 RichTextBox | 更新日期: 2023-09-27 18:17:38

我有这个方法替换(粗体)字符串中的一些单词,并在ritchtextBox中显示更改后的字符串。在最后一个字符串中,我需要用换行符替换@符号。我已经试过检查这个论坛捆绑几个解决方案,但没有工作。

我使用的方法是
private string bold(string ing)
{
    StringBuilder builder = new StringBuilder();
    ing = " " + ing + " ";
    builder.Append(@"{'rtf1'ansi");
    foreach (string word in splitwords)
    {
        var regex = new Regex(@"(?<!['w])" + word + @"(?!['w])", RegexOptions.IgnoreCase);
        ing = regex.Replace(ing, m => @"'b" + m.ToString() + @"'c0");
    }
    ing = ing.Replace(@"'b", @"'b ");
    ing = ing.Replace(@"'c0", @" 'b0");
    ing = ing.Replace("@", Environment.NewLine);
    builder.Append(ing);
    builder.Append(@"}");
     MessageBox.Show("builder.ToString():" + builder.ToString());
    return builder.ToString();
}

当我调用this方法并把它放到ritchTextBox中时,它不会打印新行

ingred.Rtf = bold(ingd);

我该如何解决这个问题??

编辑::输入字符串- line1 @ line2 @ line3

MessageBox中的

输出

Builder.ToString() : {'rtf1'ansi'b line1'b0
line2
line3
}

ritchTextBox的输出:line1 line2 line3

RichTextBox不打印换行符

代替

 ing = ing.Replace("@", Environment.NewLine);

    ing = ing.Replace("@", @"'par'r'n");

使用此代码替换

 int startIndex = 0, index;
RichTextBox myRtb = new RichTextBox(); // if have A richtextBox Remove thisline and Use your Richtextbox
            myRtb.Rtf = STRRTF;// if have A richtextBox Remove thisline and Use your Richtextbox
     while ((index = myRtb.Text.IndexOf("@", startIndex)) != -1)
                {
                    myRtb.Select(index, word.Length);
                    myRtb.SelectedText ="'n";
                    startIndex = index + 1;
                }

最好使用Environment.NewLine添加新行,确保yourRTB.MultiLine属性设置为true。将字符串赋给richtext框,就像这样yourRTB.AppendText(t)

尝试用"'r'n"字符序列替换它?

edit: is MultiLine property of ritchtextBox enabled?