删除 RichTextBox 行,同时保留 C# 中剩余行的颜色

发布于 2024-12-02 15:01:15 字数 219 浏览 0 评论 0原文

考虑一个 RichTextBox,它有 400 行,并包含许多不同颜色的单词和行。

是否可以删除该文本框的前100行,而保留剩余单词的颜色。目前,我正在使用下面的代码来删除线条,但它无法保留颜色。

if (rtb.Lines.Count() > 400)
     rtb.Lines = rtb.Lines.Skip(100).ToArray();

Consider a RichTextBox which has 400 lines and includes a number of words and lines in diffident colours.

Is it possible to remove the first 100 lines of this text box, while the colour of remaining words are reserved. Currently, I am using the below code to remove lines, but It is not able to keep colours.

if (rtb.Lines.Count() > 400)
     rtb.Lines = rtb.Lines.Skip(100).ToArray();

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

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

发布评论

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

评论(3

浅笑依然 2024-12-09 15:01:15

使用 SelectionText 属性。首先选择要删除的行,然后通过将 SelectionText 设置为空字符串来删除它们。像这样:

   richTextBox1.SelectionStart = 0;
   richTextBox1.SelectionLength = richTextBox1.GetFirstCharIndexFromLine(200);
   richTextBox1.SelectedText = "";

这保留了所有其他行的格式。这可能会导致 UI 上出现明显的闪烁,您可以通过将 Begin/EndUpdate 方法实现为 此处显示

Use the SelectionText property. First select the lines you want to remove, then remove them by setting SelectionText to an empty string. Like this:

   richTextBox1.SelectionStart = 0;
   richTextBox1.SelectionLength = richTextBox1.GetFirstCharIndexFromLine(200);
   richTextBox1.SelectedText = "";

This preserves the formatting of all the other lines. This can cause visible flicker on the UI, you can suppress that by implementing the Begin/EndUpdate methods as shown here.

强者自强 2024-12-09 15:01:15

如果您想保留格式,则不能使用 Lines 属性。 Lines 派生自 TextBoxBase。您需要使用 Rtf 属性并自己解析返回的字符串中的行。如果您只想获取行数,然后解析 RTF,那么您可以执行以下操作:

// NOTE: I am using Length rather than Count() because the array already knows its length
if (rtb.Lines.Length > 400)
{
    // Parse the rtf here to remove the unwanted lines and preserve the format
}

您需要查看 RTF 规范 准确拉出实际行。换行符由标签 \par 指示。处理起来比较棘手的行是第一行,因为它可能在实际的第一行文本之前包含额外的信息。

You can't use the Lines property if you want to preserve the formatting. Lines is derived from TextBoxBase. You need to use the Rtf property and parse the lines yourself in the string you get back. If you want to just get the line count and then parse the RTF then you could do something like:

// NOTE: I am using Length rather than Count() because the array already knows its length
if (rtb.Lines.Length > 400)
{
    // Parse the rtf here to remove the unwanted lines and preserve the format
}

You would need to look at the RTF specification to accurately pull out the actual lines. A line break is indicated by the tag \par. The line that would be tricky to deal with is the first line because it may contain extra information before the actual first line text.

放低过去 2024-12-09 15:01:15

.SelectedText = "" 在我的应用程序中引发 Windows 叮

所以我找到了第二个解决方案,即使用 .Lines 属性

if (nbLines > maxLines)
{
    Array.Copy(rtfBox.Lines, 1,
               rtfBox.Lines, 0, rtfBox.Lines.Length - 1);
}

.SelectedText = "" throws a Windows ding in my application

So I found a second solution which is to play with .Lines property

if (nbLines > maxLines)
{
    Array.Copy(rtfBox.Lines, 1,
               rtfBox.Lines, 0, rtfBox.Lines.Length - 1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文