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

前端 未结 3 1310
遥遥无期
遥遥无期 2020-12-11 23:09

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 b

3条回答
  •  一生所求
    2020-12-11 23:52

    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.

提交回复
热议问题