删除RichTextBox行,同时保持其余行的颜色在C# [英] removing RichTextBox lines while keeping the colour of remaining lines in C#

查看:2164
本文介绍了删除RichTextBox行,同时保持其余行的颜色在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

可以删除此文本框的前100行,而剩余字的颜色被保留。目前,我使用以下代码删除行,但它不能保留颜色。

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();


推荐答案

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

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 = "";

这将保留所有其他行的格式。这可能会导致UI上的可见闪烁,你可以通过实现Begin / EndUpdate方法来抑制这种情况:此处显示

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.

这篇关于删除RichTextBox行,同时保持其余行的颜色在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆