RichTextBox颜色选中的行

Pic*_*ght 9 .net c# richtextbox winforms

我是Windows Forms的新手.我正在使用VS 2008,C#编写RichTextBox.我想在写入RichTextBox时能够用不同的颜色为每一行着色.有人可以指点我的样品.谢谢

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
  }

}
Run Code Online (Sandbox Code Playgroud)

Dan*_*ant 13

SelectionColor在追加之前设置,例如:

    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++;
    }
Run Code Online (Sandbox Code Playgroud)