Click here to Skip to main content
Click here to Skip to main content

Controlling the Length of a RichTextBox in C#

, 11 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

mikey222
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

 
Generalgood article PinmemberDonsw9-Apr-09 7:49 
QuestionAny way to do it without constantly nixing selection? Pinmembersupercat912-Mar-09 8:09 
AnswerRe: Any way to do it without constantly nixing selection? Pinmembermikey22212-Mar-09 8:28 
AnswerRe: Any way to do it without constantly nixing selection? Pinmembermikey22212-Mar-09 8:32 
GeneralRe: Any way to do it without constantly nixing selection? Pinmembersupercat912-Mar-09 12:59 
GeneralRe: Any way to do it without constantly nixing selection? Pinmembermikey22212-Mar-09 18:48 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 11 Mar 2009
Article Copyright 2009 by mikey222
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid