Change RichTextBox Color

This is a discussion on Change RichTextBox Color within the C# Programming forums, part of the General Programming Boards category; I want to change specific text color in a RichTextBox. I want to insert something with a specific color. The ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Change RichTextBox Color

    I want to change specific text color in a RichTextBox. I want to insert something with a specific color. The solutions I find by googling require, if I understand correctly, that you read again all the text and then paint it accordingly. Can't you just insert something, color it and stay colored?

    The methods I use is Select() to select a text and SelectionColor to change it. It seems simple but it doesn't really work for me.

    Like, will this color blue str inserted to output rich text box?

    Code:
    output.Text += str;
    output.Select(output.Text.Length - 1 - str.Length, str.Length);
    output.SelectionColor = Color.Blue;

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If output.Text is initially empty, and str is of length 3, you'll start selection at index 3 - 1- 3 = -1. That's not what you want, is it?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Probably not. Eh, it was just a quick example, i should emmit the -1.
    What I get is color the text. But then whatever I insert to the richbox it also goes blue. I am close but can't seem to get it working.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Haven't tested anything, but it sounds like a feature, not a bug, that appended text will use the font/style/color of the last character of the existing text.
    As for a quick fix why not always set a color, black if no other is specified?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    That was my main idea. But it didn't work properly. I ll try it more and see...

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    OK.
    The RichTextBox has two properties.
    Text and Rtf.
    The text is the string it displays. The Rtf is the full string, which is the Text and some parameters about various things.
    The thing is that when you do this:
    Code:
    r.Text = r.Text;
    then every color that you have set will be lost. So I cannot do += to update the text.
    It actually just takes the Text and wraps it with a header like "{options\blah\blah USEFUL TEXT \options\blah\blah}". I you select something and change the color you will get the right thing, but when you want again to insert text it will take the updated text and wrap around a header.

    There has to be a standard way to add text with a certain color. Obviously doing:
    Code:
    r.Text += "somestring";
    doesn't work as I want, but what else can I do? I could of course create my own method for doing so, but there has to be one already

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    private void AppendColoredText(System.Windows.Forms.RichTextBox RichTextBox, string Text, System.Drawing.Color Color)
    {
    	var StartIndex = RichTextBox.TextLength;
    	RichTextBox.AppendText(Text);
    	var EndIndex = RichTextBox.TextLength;
    	RichTextBox.Select(StartIndex, EndIndex - StartIndex);
    	RichTextBox.SelectionColor = Color;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    OK. I found a workaround. But there should be a better way to do this.
    Code:
      
               Util.outputRtf   = "A basic RTF format text";
               ...
                public void Out(string str, int color)
                {
                    string s;
                    switch (color)
                    {
                        case 1:
                            s = "\\cf2 ";
                            break;
                        case 2:
                            s = "\\cf3 ";
                            break;
                        case 3:
                            s = "\\cf4 ";
                            break;
                        case 4:
                            s = "\\cf5 ";
                            break;
                        default:
                            s = "\\cf0 ";
                            break;
                    }
                    s += str;
                    s = s.Replace("\r\n", "\\par ");
                    int ind = Util.outputRtf.IndexOf("\\cf1 #");
                    Util.outputRtf = Util.outputRtf.Insert(ind, s);
                    output.Rtf = Util.outputRtf;
                    output.SelectionStart = output.Text.Length;
                    output.ScrollToCaret();
            }
    The logic is that I get a basic RTF format from a RichTextBox. I copy the ".Rtf" property value and save it to Util.outputRtf (a string inside my static Util class).
    I insert a line where the ".Text" starts. The line is "\\cf1 #". Basically i use # to indicates the end of the Text abd \\cf1 makes it white, so it doesn't show.
    The \\cfX changes colors from a list I have inserted in the Util.outputRtf string.

    This way I can insert a string in the RichTextBox and set the color from a set of colors I have.

    I could make something more complicated and general for all colors, but I don't really need it and it would be less efficient anyway.

    There are some functions out there like http://www.codeproject.com/KB/edit/csexrichtextbox.aspx

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Lol, I missed the AppendText method (slaps himself)

    Edit: Thanx Magos
    Last edited by C_ntua; 12-18-2008 at 04:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with C program
    By ChrisH in forum C Programming
    Replies: 38
    Last Post: 11-13-2004, 01:11 AM
  2. how to make user text change color
    By DarkViper in forum Tech Board
    Replies: 4
    Last Post: 12-15-2002, 05:28 PM
  3. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM
  4. Replies: 2
    Last Post: 09-04-2001, 03:12 PM
  5. is there a way to set a pixel or change font color?
    By Flikm in forum C++ Programming
    Replies: 9
    Last Post: 09-01-2001, 02:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21