删除 RichTextBox 行,同时保留 C# 中剩余行的颜色
考虑一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 SelectionText 属性。首先选择要删除的行,然后通过将 SelectionText 设置为空字符串来删除它们。像这样:
这保留了所有其他行的格式。这可能会导致 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:
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.
如果您想保留格式,则不能使用
Lines
属性。Lines
派生自 TextBoxBase。您需要使用 Rtf 属性并自己解析返回的字符串中的行。如果您只想获取行数,然后解析 RTF,那么您可以执行以下操作:您需要查看 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: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..SelectedText = "" 在我的应用程序中引发 Windows 叮
所以我找到了第二个解决方案,即使用 .Lines 属性
.SelectedText = "" throws a Windows ding in my application
So I found a second solution which is to play with .Lines property