我在 .NET 2.0 中使用 System.Windows.Forms.RichTextBox 控件,并且正在使用 c#。
我有一个要附加到 RichTextBox 的字符串数组,但我不想在 RTF 中创建新段落。我希望这些行被 \line 命令分割,而不是 RTF 中的 \par 命令。(我想模拟某人输入 shift+enter 而不是只输入。)
我无法成功让 RichTextBox.AppendText 生成 \line 命令而不是 \par 命令。所以我一直在插入我的文本,然后手动将 \line 命令插入到 RTF 本身。
当其中一个字符串包含在 RTF 中转义的字符(例如,单个 closequote 转换为 \rquote)时,这将失败,因为我无法找到插入到 RTF 中的原始字符串。
使用此解决方案,我需要先将行中的字符转义,然后再在 RTF 中搜索它。
有没有办法让 RichTextBox 控件生成 \line 命令而不是 \par 命令,或者我必须坚持使用此解决方案并搜索 RTF 转义字符串。
这是我当前的代码:
string[] lines = textInfo.Lines;
for (int i = 0; i < lines.Length; i++)
{
rtb.AppendText(lines[i]);
if (i < lines.Length - 1)
{
string rtf = rtb.Rtf;
int insertedIndex = rtf.LastIndexOf(lines[i]);
int length = lines[i].Length;
rtf = rtf.Insert(insertedIndex + length, @"\line");
rtb.Rtf = rtf;
}
}