Click here to Skip to main content
Licence CPOL
First Posted 11 Mar 2009
Views 14,737
Bookmarked 12 times

Controlling the Length of a RichTextBox in C#

By | 11 Mar 2009 | Article
This technique preserves RTF formatting when removing text from a RichTextBox control.

Introduction

I use TextBox controls in background forms for logging application events. Because the applications run for days at a time and many events are recorded, the TextBoxes need to be pruned to avoid using too much memory.

Recently, I switched to RichTextBox controls so I could use colored text, and found that my trim routine lost the text colors and other formatting. The method presented here solves that problem.

Using the code

Here is my new logging routine:

/// <summary>
/// timestamp the message and add to the textbox.
/// To prevent runtime faults should the amount
/// of data become too large, trim text when it reaches a certain size.
/// </summary>
/// <param name="text"></param>
public void AppendTrace(string text, Color textcolor)
{
    // keep textbox trimmed and avoid overflow
    // when kiosk has been running for awhile

    Int32 maxsize = 1024000;
    Int32 dropsize = maxsize / 4;

    if (richTextBox_RTCevents.Text.Length > maxsize)
    {
        // this method preserves the text colouring
        // find the first end-of-line past the endmarker

        Int32 endmarker = richTextBox_RTCevents.Text.IndexOf('\n', dropsize) + 1;
        if (endmarker < dropsize)
            endmarker = dropsize;

        richTextBox_RTCevents.Select(0, endmarker);
        richTextBox_RTCevents.Cut();
    }

    try
    {
        // trap exception which occurs when processing
        // as application shutdown is occurring

        richTextBox_RTCevents.SelectionStart = richTextBox_RTCevents.Text.Length;
        richTextBox_RTCevents.SelectionLength = 0;
        richTextBox_RTCevents.SelectionColor = textcolor;
        richTextBox_RTCevents.AppendText(
          System.DateTime.Now.ToString("HH:mm:ss.mmm") + " " + text);
    }
    catch (Exception ex)
    {
    }
}

It is called like this:

AppendTrace("some text" + Environment.NewLine, Color.Blue);

Points of Interest

The SelectionStart and SelectionLength properties are reset prior to setting the SelectionColor and appending the text; otherwise, the line added, sometimes, did not have the right color.

History

  • March 11, 2009 - Original submission.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

mikey222

Software Developer (Senior)

Canada Canada

Member

I have been developing software applications for over 25 years, ranging from device drivers and real-time applications in C and assembler to Ajax and PHP web applications.
 
Currently I am working on interactive distance learning applications using Microsoft RTC SDK. I've been using Visual Studio, C#, and SQL Server almost exclusively for the past 3 years.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalgood article PinmemberDonsw6:49 9 Apr '09  
QuestionAny way to do it without constantly nixing selection? Pinmembersupercat97:09 12 Mar '09  
AnswerRe: Any way to do it without constantly nixing selection? Pinmembermikey2227:28 12 Mar '09  
AnswerRe: Any way to do it without constantly nixing selection? Pinmembermikey2227:32 12 Mar '09  
GeneralRe: Any way to do it without constantly nixing selection? Pinmembersupercat911:59 12 Mar '09  
GeneralRe: Any way to do it without constantly nixing selection? Pinmembermikey22217:48 12 Mar '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 11 Mar 2009
Article Copyright 2009 by mikey222
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid