Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...