-1
richTextBox1.Select(0, richTextBox1.Lines[0].Length);
richTextBox1.SelectionColor = Color.Red;

The first time only line 0 the first line in richTextBox1 is in Red. But i call this lines in a timer tick event each X seconds and on the next time it's coloring all the text in the richTextBox1 in Red.

How do i make that it will keep coloring in Red only the first line ? (Lines[0] )

EDIT

This is the full code in a method im calling the method each x seconds from a timer tick event:

private void CombindedStringFix()
{
     scrollerList = new List<string>(TextExtractor.newTextWithoutLinks);
     scrollerText = string.Join(Environment.NewLine, scrollerList);
     combindedString = string.Join(Environment.NewLine, newText);
     scroller1.TextToScroll = scrollerText;
     richTextBox1.Text = combindedString.TrimStart();
     richTextBox1.Refresh();
     string[] rlines = richTextBox1.Lines;
     richTextBox1.SelectionStart = 0;
     richTextBox1.SelectionLength = rlines[0].Length;
     richTextBox1.SelectionColor = Color.Red;
     richTextBox1.Select(rlines[0].Length, rlines[1].Length + 1);
     richTextBox1.SelectionColor = Color.Green;
}

First time it's coloring the top first line in red second top line in green fine. But in the next timer itertion it's coloring all the text in the richTextBox1 in Red.

brothers28
  • 1,196
  • 17
  • 23
  • Works perfectly fine for me as well. What are you doing. Are you setting the entire text each time instead of using the AppendText? – Piyush Jul 07 '14 at 05:54
  • P.K updated my question. 'm not sure if that's the problem but this is the method i call from a timer tick event each x seconds and also where i want to color the lines. The same the code is now and if it was like before it does color first time the lines in red and green but in the next timer tick event it will color in Red all the text. – user3809278 Jul 07 '14 at 07:24
  • [How to use multi color in richtextbox?][1] [1]: http://stackoverflow.com/questions/13220856/how-to-use-multi-color-in-richtextbox – Meysam Tolouee Jul 07 '14 at 07:39

1 Answers1

1

When you replace the Text property of a RichTextBox control, you aren't replacing the current format. The format of the first character of the control is set to Red on the first pass through. When you replace the Text of the control in the subsequent call, it's getting that value and applying it to the rest of the text in the control.

To work around that issue, use the Clear() method first:

richTextBox1.Clear();
richTextBox1.Text = combindedString.TrimStart();
LarsTech
  • 80,625
  • 14
  • 153
  • 225