Click here to Skip to main content
15,891,734 members
Articles / Desktop Programming / Windows Forms

Controlling the Length of a RichTextBox in C#

Rate me:
Please Sign up or sign in to vote.
3.43/5 (5 votes)
11 Mar 2009CPOL 46.6K   14   7
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:

C#
/// <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:

C#
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)


Written By
Software Developer (Senior)
Canada Canada
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.

Comments and Discussions

 
Questionviolate clipboard Pin
NikolaB25-Mar-15 3:39
NikolaB25-Mar-15 3:39 
Generalgood article Pin
Donsw9-Apr-09 6:49
Donsw9-Apr-09 6:49 
QuestionAny way to do it without constantly nixing selection? Pin
supercat912-Mar-09 7:09
supercat912-Mar-09 7:09 
AnswerRe: Any way to do it without constantly nixing selection? Pin
mikey22212-Mar-09 7:28
mikey22212-Mar-09 7:28 
AnswerRe: Any way to do it without constantly nixing selection? Pin
mikey22212-Mar-09 7:32
mikey22212-Mar-09 7:32 
GeneralRe: Any way to do it without constantly nixing selection? Pin
supercat912-Mar-09 11:59
supercat912-Mar-09 11:59 
GeneralRe: Any way to do it without constantly nixing selection? Pin
mikey22212-Mar-09 17:48
mikey22212-Mar-09 17:48 

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

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