简体   繁体   中英

Color different texts of a RichTextBox string in C#

I want to coloured different parts of a richtextbox with different colors, same as posted here .

But with one difference, in my case I need to use the method Insert of the richtextbox to insert message at the top.

I was using classical method:

myRichTextBox.Text.Insert(0, myMessage);

but using it I cannot coloured different parts of the message so I have done below extension methods:

public static class RichTextBoxExtensions
{
    public static string Insert(this string str, int index, RichTextBox box, string text, Color color)
    {
        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionColor = color;
        //box.Text = str.Insert(0, box, text, color);
        box.Text = str.Insert(index, text);
        box.SelectionColor = box.ForeColor;

        return box.Text;
    }
}

and then I use it by calling in the following way from any point of my program:

    private void PrintMessage(RichTextBox box, string message, Color color)
    {
        box.Text = box.Text.Insert(0, box, message, color);
    }

But it is not working, it throws an exception in extension method Insert in line:

box.Text = str.Insert(0, box, text, color);

It says "Stack overflow". Any ideas?

UPDATED :

    private void PrintMessage(RichTextBox box, string message, Color color)
    {
        box.SelectedText = box.SelectedText.Insert(0, box, message, color);
    }

public static class RichTextBoxExtensions
{
    public static string Insert(this string str, int index, RichTextBox box, string text, Color color)
    {
        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionColor = color;
        box.SelectedText = str.Insert(index, text);
        box.SelectionColor = box.ForeColor;

        return box.Text;
    }
}

But it does not work, text does not appear coloured.

NOTE :

I need text to be added at the top of the richtextbox control, not at the bottom, so I was using Insert instead of AppendText.

UPDATE 2 : Using AppendText and setting SelectionStart to 0 to append text at the top of the richtextbox as TaW says here , fails the first time you print a message into the richtextbox. Rest of time (not the first) is going ok.

So how to make it work also for the first string appended into the richtextbox?

    public static void AppendText(this RichTextBox rtb, string text, Color color)
    {            
        rtb.SelectionStart = 0;            // set the cursor to the target position
        rtb.SelectionLength = 0;           // nothing selected, yet
        rtb.SelectedText = text;           // this inserts the new text 
        rtb.SelectionLength = text.Length; // now we prepare the new formatting
        rtb.SelectionColor = color;
    }

To Add text at the top with color change, I used this code, for me it is the best solution. It does not use copy/paste or text deletion and rewriting.

 public void Log(RichTextBox myTextBox, string Testo, Color colore)
    {
            myTextBox.SelectionStart = 0;
            myTextBox.SelectionLength = 0;
            myTextBox.SelectionColor= colore;
            myTextBox.SelectedText = Testo;
    }

You wrote the prototype wrong and implementation is not right. I made a working code. All you need is to add a Richtextbox element in your Form and name it box.

    using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public static class RichTextBoxExtensions
{
    public static void AppendText(this RichTextBox box, string text, Color color)
    {
        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionColor = color;
        box.AppendText(text);
        box.SelectionColor = box.ForeColor;
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        box.AppendText("[" + DateTime.Now.ToShortTimeString() + "]", Color.Red);
        box.AppendText(" ");
        box.AppendText("GREEN TEXT", Color.Green);
        box.AppendText(": ");
        box.AppendText("BLUE TEXT", Color.Blue);
        box.AppendText(Environment.NewLine);
    }
}
}

To call it in your code, make it like this

private void PrintMessage(RichTextBox box, string message, Color color)
 {
     box.Text = box.AppendText(message, color);
 }

在此处输入图片说明

Code taken from Richtextbox prepend new text with color

UPDATE:: To write on top of Richtextbox used this modified code (i did a dirty fix using clipboard) but it works

        public static void AppendTextToTop(this RichTextBox box, string text, Color color)
    {
        Clipboard.Clear();
        box.SelectAll();
        box.Copy();
        box.Clear();
        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionColor = color;
        box.AppendText(text);
        box.SelectionColor = box.ForeColor;

        box.Paste();
    }

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