how do i change the font color of a String.SubString in a textBox?

for example txtBox.text = "The color is green";

i want to display the word green as "green". is there a way to do that




Answer this question

how do i change the font color of a String.SubString in a textBox?

  • Vurg

    You can't do this in the TextBox control, only in the RichTextBox control.

    Use the Select method to select a piece of text and set the color. Then re-select something else and set the color.


    if( myRichTextBox.TextLenght >= 5 )
    {
    myRichTextBox.Select( 0, 5 );
    myRichTextBox.SelectionColor = Color.Green;
    }

    if( myRichTextBox.TextLenght >= 15 )
    {
    myRichTextBox.Select( 10, 15 );
    myRichTextBox.SelectionColor = Color.Red;
    }



  • RayCh

    ok, thanks, that's weird, we're discussing similar problems in different threads hehe.

  • Dan Kahler

    thanks i want to use rtf format but i don't know how to extract the plain text from the rtf code and display it in a reportviewer.

    anyway i have decided to use delimeters, "/*" and "*/", how can i find their indices if they're all over the rtb



  • mina_mina

    You must search the plain text for the tag's. Manipulating RTB yourself is a little bit hard, use a RichTextBox control for this and only use the Text property to read the plain text and change color, fonts and style with the selection methods.


  • Nitron

    i got the change color part, here's what i have:

    this.txt.Select(start, len);

    this.txt.SelectionColor = Color.Green;

    now my problem is how can i highlight or select multiple substrings from txt.Text



  • how do i change the font color of a String.SubString in a textBox?