views:

1961

answers:

2

Hello,

I am new to windows Forms. I am using VS 2008, C# to write a RichTextBox. I want to be able to color each line with a different color as I write to the RichTextBox. Can someone point me to samples. Thanks

foreach (string file in myfiles)
{
  // As I process my files
  // richTextBox1.Text += "My processing results";
  if(file == "somefileName")
  {
    // Color above entered line or enter new colored line
  }

}
+3  A: 

Set SelectionColor before you append, something like:

    int line = 0;
    foreach (string file in myfiles)
    {
        // Whatever method you want to choose a color, here
        // I'm just alternating between red and blue
        richTextBox1.SelectionColor = 
            line % 2 == 0 ? Color.Red : Color.Blue;

        // AppendText is better than rtb.Text += ...
        richTextBox1.AppendText(file + "\r\n");
        line++;
    }
Daniel LeCheminant
A: 

brilliant code...

I didn't look for this code... But I did save it for further use...

It's so simple, yet improves the readability of tables, texts ... this feature was always defined as "nice to have"... so every time I wanted to do something like this, I skipped it, because of its low priority - can't believe it's so simple.

Thanks ;)