2009-02-23 84 views
9

我是新的windows窗体。我使用VS 2008,C#编写RichTextBox。 我希望能够在写入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 
    } 

} 

回答

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++; 
    } 
+0

+1。 VB.Net用户应该记住,\ r \ n正在C#中转义。在VB中写入.AppendText(file&vbCrLf) – smirkingman 2012-11-28 11:02:31