如何将 \line 而不是 \par 附加到 System.Windows.Forms.RichTextBox

发布于 2024-07-25 10:45:37 字数 921 浏览 5 评论 0原文

我正在使用 .NET 2.0 中的 System.Windows.Forms.RichTextBox 控件并使用 c#。

我有一个字符串数组,我想将其附加到 RichTextBox,但我不想在 RTF 中创建新段落。 我希望 RTF 中的行由 \line 命令划分,而不是 \par 命令。 (我想模拟某人输入 shift+enter 而不是仅输入。)

我无法成功让 RichTextBox.AppendText 生成 \line 命令而不是 \par 命令。 所以我一直在插入文本,然后手动将 \line 命令插入 RTF 本身。

当其中一个字符串包含 RTF 中转义的字符(例如,单个闭引号转换为 \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;
    }
}

I am using the System.Windows.Forms.RichTextBox control in .NET 2.0 and am using c#.

I have an array of strings that I would like to append to the RichTextBox, but I do not want to create new paragraphs in the RTF. I want the lines to be divided by \line commands, not \par commands in the RTF. (I want to simulate someone typing shift+enter instead of just enter.)

I have not been able to successfully get RichTextBox.AppendText to generate a \line command instead of a \par command. So I have been inserting my text and then manually inserting the \line command into the RTF itself.

This fails when one of the strings contains characters that are escaped in the RTF (a single closequote is converted to \rquote for example) because I am not able to find the original string I inserted into the RTF.

With this solution, I will need to escape out the characters in my line before searching for it in the RTF.

Is there a way to get the RichTextBox control to generate the \line command and not the \par command or will I have to stick with this solution and search for the RTF escaped string.

Here is my current code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2024-08-01 10:45:37

当您修改 Text 和 Rtf 属性中的任何一个时,RichTextBox 控件会自动更新它们。 附加到 Text 属性的任何文本都将自动更新为 RTF 属性。

从一些快速测试来看,RichTextBox 控件似乎自动将 \par 插入到任何换行符(控件上的 Enter、Shift-Enter 和 Text 属性上的 \r \n \r\n Environment.NewLine 以及 Text 属性上的 AppendText() 函数)控制自己)。 这就是您报告的问题。

我不相信有任何方法可以改变控件生成 RTF 的方式。 我只需附加文本并让 RichTextBox 控件处理可能需要转义的任何字符的转换,然后将所有 \par 替换为 \line。

假设 textInfo 是一个 TextBox 并且您想要将其内容附加到一些已经存在的 RTF 的末尾(如果不替换 += for = 在第 1 行

rtb.Text += textInfo.Text;

rtb.Rtf = rtb.Rtf.Replace(@"\par", @"\line");

编辑:

因为您逐行将行附加到文档中,并且您知道控制为 \r\n 生成一个 \par ,你可以在你想要用 \line 中断的行后缀一些独特的文本(你可以在这里发挥创意,我使用 REPLN)。确定要替换的行,我假设您从您的问题中执行此操作,然后您可以再次将 REPLN\par 替换为 \line ,这不像我希望的那样优雅,但它得到了任务完成。

foreach (string line in textInfo.Lines)
{
   //assuming your condition is that the string contains the substring, "substring"
   rtb.AppendText(line + (line.Contains("substring") ? "REPLN\r\n" : "\r\n"));
}

rtb.Rtf = richTextBox1.Rtf.Replace(@"REPLN\par", @"\line");

The RichTextBox control automatically updates the Text and Rtf properties when you modify either of them. Any text appended to the Text property will automatically be updated to the RTF property.

From some quick testing, it seems the RichTextBox control automatically inserts \par to any newline (enter, shift-enter on the control and \r \n \r\n Environment.NewLine on the Text property and the AppendText() function on the control itself). Which is what you report as your problem.

I don't believe there is any way to change how the control generates RTF. I would just append the text and let the RichTextBox control handle the conversion of any characters that may need escaping and then replace all the \par with \line.

Assuming textInfo is a TextBox and you want to append its contents to the end of some already existing RTF (if not replace += for = in line 1

rtb.Text += textInfo.Text;

rtb.Rtf = rtb.Rtf.Replace(@"\par", @"\line");

Edit:

Since you're appending lines to your document line by line, and you know the control generates a \par for \r\n, you could postfix some unique text(you can get creative here, I used REPLN) to the lines you want to break with a \line. Of course, you'd have to be able to identify the lines you want to be replaced, which I assume you do from your question. After appending your unique text, you can then replace again REPLN\par with \line. Not as elegant as I would have liked, but it gets the job done.

foreach (string line in textInfo.Lines)
{
   //assuming your condition is that the string contains the substring, "substring"
   rtb.AppendText(line + (line.Contains("substring") ? "REPLN\r\n" : "\r\n"));
}

rtb.Rtf = richTextBox1.Rtf.Replace(@"REPLN\par", @"\line");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文