[英]Truncate text in RichTextBox and TextBox
I am using RichTextBox
and TextBox
for showing some information which is collected during several days. 我正在使用
RichTextBox
和TextBox
来显示几天收集的一些信息。 So there are a lot of strings inside it after couple days and I get OutOfMemory
exception. 因此,几天后它里面有很多字符串,我得到
OutOfMemory
异常。 I think this error occurs because of lots of data. 我认为发生此错误是因为有大量数据。 Is there some properties or functions which allow to limit number of strings inside
RichTextBox
and TextBox
? 是否有一些属性或函数可以限制
RichTextBox
和TextBox
内部的字符串数? I need to truncate only old strings which are in the beggining of list. 我只需要截断列表开头的旧字符串。 For instance, take a look at picture below:
例如,看下面的图片:
Any ideas? 有任何想法吗?
I created simple code which allows me to resolve this problem. 我创建了简单的代码,可以解决这个问题。 For
TextBox
: 对于
TextBox
:
if (limitLines>0 && simpleTextBox.LineCount > limitLines)
{
string tempText = "";
for (int i = simpleTextBox.LineCount-limitLines; i < simpleTextBox.LineCount; i++)
{
tempText += simpleTextBox.GetLineText(i);
}
simpleTextBox.Clear();
simpleTextBox.Text = tempText;
}
simpleTextBox.AppendText(data);
For RichTextBox
: 对于
RichTextBox
:
TextRange tr = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
tr.Text = text + "\r\n";
tr.ApplyPropertyValue(TextElement.ForegroundProperty, solidColorBrush);
if (limitLines > 0 && richTextBox.Document.Blocks.Count > limitLines)
{
for (int i = richTextBox.Document.Blocks.Count - limitLines; i < richTextBox.Document.Blocks.Count; i++)
richTextBox.Document.Blocks.Remove(richTextBox.Document.Blocks.FirstBlock);
}
I hope it helps to someone else! 希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.