2009-02-23 1 views
9

私はWindowsフォームが初めてです。私はリッチテキストボックスを書くために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 
    } 

} 

答えて

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ユーザーは、C#で\ r \ nがエスケープしていることを覚えておく必要があります。 .AppendText(file&vbCrLf)VBで書く – smirkingman

+0

コードコメントで述べたように、+ =を使用すると、既にボックスに設定されているすべてのテキストの色がリセットされているように見えるので、AppendTextメソッドはそのトリックを行いました。 – kad81

関連する問題