Menu Close

Set Different text color in RichTextBox control

The Windows Forms RichTextBox control is used for displaying, entering, and manipulating text with formatting. The RichTextBox control does everything the TextBox control does, but it can also display fonts, colors, and links; load text and embedded images from a file; and find specified characters. The RichTextBox control is typically used to provide text manipulation and display features similar to word processing applications such as Microsoft Word.

 How to set the text colour

1. Set all the text in the same colour

There are two methods to the text colour:

1)      Set the text colour in code:

this->rtxbx->ForeColor = Color::Blue; //set it as Blue colour

 2)      Set the text colour from the property window

  2. Set different colour for  each text message1). The function is used to set the colour for each text message.

void AppendText(RichTextBox^ box, String^ text, Color color)

{

    box->SelectionLength = 0;

    box->SelectionStart = box->TextLength;

     box->SelectionColor = color;

    box->AppendText(text);

    box->SelectionColor = box->ForeColor;

}

 2)      Call the function to set the text colour

For example:

this->rtxbx->Text = “”;

AppendText(this->rtxbx, “Text in Red colour”, Color::Red);

AppendText(this->rtxbx, “Text in Blue colour”, Color::Blue);

AppendText(this->rtxbx, “Text in Yellow colour”, Color::Yellow);

AppendText(this->rtxbx, “Text in Black colour”, Color::Black);

 

One comment on “Set Different text color in RichTextBox control

  1. I just couldn’t depart your website before suggesting that I extremely loved the
    standard info a person supply to your guests? Is going to
    be back often in order to check up on new posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

18,519 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>