简体   繁体   中英

Truncate text in RichTextBox and TextBox

I am using RichTextBox and TextBox for showing some information which is collected during several days. So there are a lot of strings inside it after couple days and I get OutOfMemory exception. 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 ? 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 :

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 :

        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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM