none
How to Add different colors to rich text box.

    Question

  • Hi,

    I would like to add different colors for different text inside a textbox / richtextbox / any other similar control.
    A simple example is in Yahoo Messenger, we can see different color for the user name.

    Thanks in Advance!!!
    Thursday, July 24, 2008 10:14 AM

Answers

  • Yes, RTB can be a pain like this.  You are replacing all of the text in the control.  Which makes it forget the previously applied attributes, there is no practical way it could map the old attributes to the new text.  Fix it like this:

          richTextBox1.AppendText("Color1" + Environment.NewLine);


    Hans Passant.
    Thursday, July 24, 2008 1:30 PM
    Moderator

All replies

  • You'll have to use the SelectionBackColor and SelectionForeColor properties.  Select the text first with SelectionStart and SelectionLength.
    Hans Passant.
    Thursday, July 24, 2008 10:24 AM
    Moderator
  •             int startLength = richTextBox1.Text.Length; 
                richTextBox1.Text = richTextBox1.Text + "Color1" + Environment.NewLine; 
                int endLength = richTextBox1.Text.Length; 
                richTextBox1.SelectionBackColor = Color.Red; 
                richTextBox1.SelectionStart = startLength; 
                richTextBox1.SelectionLength = (endLength - startLength); 
    I used to the above code in the button click handler,  but i'm not getting different color inside the same control.

    I need to display text with different color.
    Thursday, July 24, 2008 11:22 AM
  • Close.  First select, then use SelectionBackColor.
    Hans Passant.
    Thursday, July 24, 2008 11:25 AM
    Moderator
  •         private void button1_Click(object sender, EventArgs e) 
            { 
                int startLength = richTextBox1.Text.Length; 
                richTextBox1.Text = richTextBox1.Text + "Color1" + Environment.NewLine; 
                int endLength = richTextBox1.Text.Length; 
                richTextBox1.SelectionStart = startLength; 
                richTextBox1.SelectionLength = (endLength - startLength); 
                richTextBox1.SelectionColor = Color.Red; 
            } 
     
            private void button2_Click(object sender, EventArgs e) 
            { 
                int startLength = richTextBox1.Text.Length; 
                richTextBox1.Text = richTextBox1.Text + "Color2" + Environment.NewLine; 
                int endLength = richTextBox1.Text.Length; 
                richTextBox1.SelectionStart = startLength; 
                richTextBox1.SelectionLength = (endLength - startLength); 
                richTextBox1.SelectionColor = Color.Blue; 
            } 
     
    In my Form, i have added two buttons and one Richtextbox. When i click one button, i should get the text in red color and if i click the other button, i should get the text with another color. But when i follow this code i'm not getting those color.

    Thursday, July 24, 2008 1:02 PM
  • Yes, RTB can be a pain like this.  You are replacing all of the text in the control.  Which makes it forget the previously applied attributes, there is no practical way it could map the old attributes to the new text.  Fix it like this:

          richTextBox1.AppendText("Color1" + Environment.NewLine);


    Hans Passant.
    Thursday, July 24, 2008 1:30 PM
    Moderator
  • YOur startLength and endLength are the same
    Thursday, March 27, 2014 2:10 AM