Friday, May 6, 2011

how to change colour of newly added text in Rich Text box

Consider my windows application built using C# VS 2005 displays certain contents in a Rich Text Box. Now when i add some new text, it should appear in a different colour. i.e i need to differentiate the text which gets displayed programmatically and the text that i enter in the text box. how can i do this.?

i.e the font color of the newly inserted text should be different from the text that is already displayed.

PS: Previously i have used text box and changed it to rich text box as told in the below thread. link text

From stackoverflow
  • IIRC, this works:

    1. Set the selection start index to the last character.
    2. Change selection color.
    3. Add new text.
    4. Reset selection color.
  • private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {            
            this.richTextBox1.SelectionColor = Color.Red;
        }
    

    Text inserted by keyboard is red.

0 comments:

Post a Comment