17

I using C# windows forms and I have richtextbox and I want to color some text in red, some in green and some in black.

How to do so? Image attached.

enter image description here

Omar
  • 16,329
  • 10
  • 48
  • 66
Billie
  • 8,938
  • 12
  • 37
  • 67
  • Some code that shows what you already did would be helpful. – Paul Hiemstra Nov 04 '12 at 17:30
  • @PaulHiemstra Indeed I'm a C++, C, Java, Assembly and more langs programmer, I had never used C# before. I take a course in the collage about C#. 'Till I'll get in the business , I might need your help. thank you for listening. – Billie Nov 05 '12 at 19:32
  • I hope here the solution you are looking for http://stackoverflow.com/a/27149285/998483 – Boobalan Nov 26 '14 at 12:39

3 Answers3

34

System.Windows.Forms.RichTextBox has got a property of type Color of the name SelectionColor which gets or sets the text color of the current selection or insertion point. You can use this property to mark specific fields in your RichTextBox with the colors you specify.

Example

RichTextBox _RichTextBox = new RichTextBox(); //Initialize a new RichTextBox of name _RichTextBox
_RichTextBox.Select(0, 8); //Select text within 0 and 8
_RichTextBox.SelectionColor = Color.Red; //Set the selected text color to Red
_RichTextBox.Select(8, 16); //Select text within 8 and 16
_RichTextBox.SelectionColor = Color.Green; //Set the selected text color to Green
_RichTextBox.Select(0,0); //Select text within 0 and 0

Notice that: You may avoid calculations by using RichTextBox.Find(string str) which can be added through Object Browser if you would like to highlight the text within the Lines in RichTextBox giving it's value

Example

RichTextBox _RichTextBox = new RichTextBox(); //Initialize a new RichTextBox of name _RichTextBox
_RichTextBox.Find("Account 12345, deposit 100$, balance 200$"); //Find the text provided
_RichTextBox.SelectionColor = Color.Green; //Set the selected text color to Green

Thanks,
I hope you find this helpful :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • This is a working solution but there is an easyer way to proceed : http://stackoverflow.com/a/10587765/1529139 – 56ka Feb 17 '14 at 10:15
13

I found this extension method that gives you the ability to change the color of the string as well as inserting a newline value:

    public static void AppendText(this RichTextBox box, string text, Color color, bool AddNewLine = false)
    {
        if (AddNewLine)
        {
            text += Environment.NewLine;
        }

        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionColor = color;
        box.AppendText(text);
        box.SelectionColor = box.ForeColor;
    }
Mark Kram
  • 5,672
  • 7
  • 51
  • 70
  • I think you can also add `box.SuspendLayout()` at the beginning of the method and `box.ResumeLayout()` at the end. – tedebus Feb 15 '17 at 14:17
-1

you can use Run object to change the color at runtime

private Run GetForegroundColor(string strInformation, Brush color)
        {
            Run noramlRun = new Run(strInformation);
            noramlRun.Foreground = color;
            return noramlRun;
        }

for more complex scenario like change the color based on requirement then visit blow link

https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/customwpfrichtextboxwithcolorchangeandhighlightfunctionality

Boobalan
  • 815
  • 11
  • 11