Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

colors - removing RichTextBox lines while keeping the colour of remaining lines in C#

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...