none
RichTextBox : Text color

    Question

  • hi..
    i have a richtextbox in which im displaying some messages, i want to change the color of some text . is there  any way to that ?
    thanx
    Tuesday, June 13, 2006 10:29 AM

Answers

  • Hi,
    yes you can, select the text and use the SelectionFont and/or SelectionColor property:

    richtextbox.SelectionFont = new Font("Verdana", 10, FontStyle.Regular);
    richtextbox.SelectionColor = Color.Blue;

    Tuesday, June 13, 2006 11:08 AM
  •  

    For Every string you want to change the font and color replace it like the following;


    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);

    string mystring=@"my first string";

    if (richTextBox1.Find(mystring)>0)

    {

    int my1stPosition=richTextBox1.Find(mystring);

    richTextBox1.SelectionStart=my1stPosition;

    richTextBox1.SelectionLength=mystring.Length;

    richTextBox1.SelectionFont=fnt;

    richTextBox1.SelectionColor=Color.CadetBlue;

    }


    If you have more then one string which will have the same font and color you can save them in an ArrayList and then apply "foreach" loop.

     

    Tuesday, June 13, 2006 11:30 AM

All replies

  • Hi,
    yes you can, select the text and use the SelectionFont and/or SelectionColor property:

    richtextbox.SelectionFont = new Font("Verdana", 10, FontStyle.Regular);
    richtextbox.SelectionColor = Color.Blue;

    Tuesday, June 13, 2006 11:08 AM
  •  

    For Every string you want to change the font and color replace it like the following;


    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);

    string mystring=@"my first string";

    if (richTextBox1.Find(mystring)>0)

    {

    int my1stPosition=richTextBox1.Find(mystring);

    richTextBox1.SelectionStart=my1stPosition;

    richTextBox1.SelectionLength=mystring.Length;

    richTextBox1.SelectionFont=fnt;

    richTextBox1.SelectionColor=Color.CadetBlue;

    }


    If you have more then one string which will have the same font and color you can save them in an ArrayList and then apply "foreach" loop.

     

    Tuesday, June 13, 2006 11:30 AM
  • if i want to color all the occurance of that string, then wat i need to do
    Thursday, June 15, 2006 6:42 AM
  • One way would be to go through every word that you want to color, select it, and change its color. Another way would be to analyse just the word that you edited and check if it needs to be changed.
    Thursday, June 15, 2006 10:00 AM
  • I had to make a correction:

    if (richTextBox1.Find(mystring) == 0)


    Thank you.
    Tuesday, April 07, 2009 12:53 PM
  • Here is a solution to search a word then chaning its style and color, then find next:

    int pos = 0;
    pos = rtb.Find(searchText, pos, RichTextBoxFinds.MatchCase);
    while (pos != -1)
    {
     if (rtb.SelectedText == searchText && tbxEditor.SelectedText != "")
     {
     rtb.SelectionLength = searchText.Length;
     rtb.SelectionFont = new Font("arial",8,FontStyle.Underline);
     rtb.SelectionColor = Color.Green;
     }
     pos = rtb.Find(searchText, pos + 1, RichTextBoxFinds.MatchCase);
    }
    
    • Edited by Hung Dao Friday, June 25, 2010 9:47 AM correct
    Friday, June 25, 2010 9:46 AM
  • mabye you can make forecolor proprety

    RichTextBox1.ForeColor = Color.Red;

    Sunday, August 19, 2012 5:30 AM