Home > DeveloperSection > Forums > RichTextBox - Add text to top with multiple colors (only latest line is showing)
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    September-04-2013 2:31 AM

 C# C# 
Ratings:


 1 Reply(s)
 1133  View(s)
Rate this:

I'm trying to replicate a log window, so the most recent log should appear at the top - most visible. Thus, I need to add a text to the top (no problem) but with multiple colors (problem).

First I store the original text. (it's rtf or text - tried both) And then I add the new text, with a username and then a message. The username should be one color and the message another. It's always single lined too.

All I get by my method is that when appending the old text or the old RTF text, the latest "log" only shows.

public void AddLog(Log log)

{

        try

        {

            string oldText = this.richTextBox1.Rtf;

            this.richTextBox1.Text = log.User + ": " + log.Message + "\n";

            this.richTextBox1.Select(0, log.User.Length);

            this.richTextBox1.SelectionColor = Color.GreenYellow;

            this.richTextBox1.Select(log.User.Length + 2, log.Message.Length);

            this.richTextBox1.SelectionColor = Color.White;

            this.richTextBox1.DeselectAll();

            this.richTextBox1.Rtf += oldText;

        }

        catch { }

}

Is this even possible? Because it doesn't save the old RTF text and the old RTF text can't be appended after the new text, which means I probably have to add newest text below which isn't what I want.

If I instead of saving the "RTF" text, the format (colors) will disappear and will only show one color.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-04-2013 7:07 AM

Hi Marcel,

Try this

public void AddLog(Log log)

{

    try

    {

        richTextBox1.SelectAll();           

        string oldText = this.richTextBox1.SelectedRtf;

        this.richTextBox1.Text = log.User + ": " + log.Message + "\n";

        this.richTextBox1.Select(0, log.User.Length);

        this.richTextBox1.SelectionColor = Color.GreenYellow;

        this.richTextBox1.Select(log.User.Length + 2, log.Message.Length);

        this.richTextBox1.SelectionColor = Color.White;

        this.richTextBox1.DeselectAll();

        this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;

        this.richTextBox1.SelectedRtf = oldText;

        this.richTextBox1.DeselectAll();

    }

    catch { }

}



Don't want to miss updates? Please click the below button!

Follow MindStick