2009-02-23 92 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