Click here to Skip to main content
15,881,281 members
Articles / Desktop Programming
Tip/Trick

Windows Form: Limit Number of Lines in RichTextBox Control C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
8 Feb 2019CPOL 14.8K   200   1   3
Limit number of lines in a RichTextBox

Introduction

Earlier today, I had a requirement to only show 10 lines (as needed) in a RichTextBox, removing the oldest as new lines were been added, and maintaining proper line formats. I made a simple solution for that, and today, I am going to talk about that.

Example

Here, we can see the differences between the new custom append and regular default append.

Image 1

Using the Code

The helper class with a new extension method:

C#
public static class ControlHelper
{
    public static void AddLine(this RichTextBox box, string text, uint? maxLine = null)
    {
        string newLineIndicator = "\n";

        /*max line check*/
        if (maxLine != null && maxLine > 0)
        {
            if (box.Lines.Count() >= maxLine)
            {
                List<string> lines = box.Lines.ToList();
                lines.RemoveAt(0);
                box.Lines = lines.ToArray();
            }                
        }

        /*add text*/
        string line = String.IsNullOrEmpty(box.Text) ? text : newLineIndicator + text;
        box.AppendText(line);
    }
}

New Line Append With Max Limit

Using this extension method to append a new line in the control with line limit:

C#
this.Invoke((MethodInvoker)delegate
{
    richTextBox1.AddLine("Hello!", 10);
    richTextBox1.ScrollToCaret();
});

New Line Append

Using this extension method to append a new line in the control without any limit:

C#
this.Invoke((MethodInvoker)delegate
{
    richTextBox2.AddLine("Hello!");
    richTextBox2.ScrollToCaret();
});

Default Text Append

This is how default RichTextBox works:

C#
this.Invoke((MethodInvoker)delegate
{
    richTextBox3.AppendText("Hello!");
    richTextBox3.ScrollToCaret();
});

Please find the sample desktop project (VS2017) as an attachment.

License

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


Written By
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAdding line when removing previous line Pin
asheimo10-Sep-20 12:03
asheimo10-Sep-20 12:03 
QuestionWhat if the new lines come from another thread? Pin
Member 147391388-Feb-20 17:59
Member 147391388-Feb-20 17:59 
AnswerRe: What if the new lines come from another thread? Pin
DiponRoy9-May-20 3:31
DiponRoy9-May-20 3:31 
I would do something like this

1. Use a separate manager class to hold a message queue, aa add message method, a start print method, a process running flag

message queue:
holes not printed messages
process running flag:
true means, queue message printing in progress.
add message method:
adds a message to the queue
start print method:
If the state flag is true, it will do nothing.

If the flag is false, set its value to true.
Dequeue messages for the queue and print it until the queue is emptry.
Set flag value to false

2. Create an instance of that class inside the constructor or as preferable

3. Use delegate and event for each thread manager class
4. Inside event implementation method, call the add message method and start print method of that instance

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.