RichTextBox 不打印换行符
RichTextBox not printing newline
我有这个方法可以替换(粗体)字符串中的一些单词并在 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();
}
当我在 ritchTextBox 中调用此方法和 "put it" 时,它不打印新行
ingred.Rtf = bold(ingd);
我该如何解决??
编辑::输入字符串 - line1 @ line2 @ line3
在消息框中输出
Builder.ToString() : {\rtf1\ansi\b line1\b0
line2
line3
}
在 ritchTextBox 中输出:line1 line2 line3
尝试用 "\r\n"
字符序列替换它?
编辑:是否启用了 ritchtextBox
的 MultiLine
属性?
最好使用 Environment.NewLine
添加新行 确保 yourRTB.MultiLine
属性 设置为 true。像这样将字符串分配给富文本框 yourRTB.AppendText(t)
使用此代码进行替换
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;
}
而不是
ing = ing.Replace("@", Environment.NewLine);
尝试
ing = ing.Replace("@", @"\par\r\n");
我有这个方法可以替换(粗体)字符串中的一些单词并在 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();
}
当我在 ritchTextBox 中调用此方法和 "put it" 时,它不打印新行
ingred.Rtf = bold(ingd);
我该如何解决??
编辑::输入字符串 - line1 @ line2 @ line3
在消息框中输出
Builder.ToString() : {\rtf1\ansi\b line1\b0
line2
line3
}
在 ritchTextBox 中输出:line1 line2 line3
尝试用 "\r\n"
字符序列替换它?
编辑:是否启用了 ritchtextBox
的 MultiLine
属性?
最好使用 Environment.NewLine
添加新行 确保 yourRTB.MultiLine
属性 设置为 true。像这样将字符串分配给富文本框 yourRTB.AppendText(t)
使用此代码进行替换
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;
}
而不是
ing = ing.Replace("@", Environment.NewLine);
尝试
ing = ing.Replace("@", @"\par\r\n");