4

If I want to upload a text file into the textbox and want to highlight certain words with a font color change, i know that i need to write TextBox.ForeColor = Color.SomeColor;
But if i want that not all the text will be in the same color, only some Substrings.
How can I do that?

1
  • 1
    RichTextBox Commented Jan 2, 2013 at 10:02

2 Answers 2

7

Check the answer by Pieter Joost van de Sande.

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

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

if( myRichTextBox.TextLenght >= 15 )
{
myRichTextBox.Select( 10, 15 );
myRichTextBox.SelectionColor = Color.Red;
}
Sign up to request clarification or add additional context in comments.

Comments

0

As @syed-mohsin answered, it's possible to add text, then select portions of the text and change its color.

It's also possible to not select anything, set SelectionColor, and any appended text (eg through AppendText or Text +=) will have that color, until you change SelectionColor again. For example:

richTextBox.AppendText("default color"); richTextBox.SelectionColor(Color.Green); richTextBox.AppendText("that will be in green"); richTextBox.SelectionColor(Color.Red); richTextBox.AppendText("that will be in red"); richTextBox.SelectionColor(Color.Black); richTextBox.AppendText("that will be in black");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.