RichTextBox color selected lines

后端 未结 1 1164
旧巷少年郎
旧巷少年郎 2020-12-30 11:07

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 some

1条回答
  •  醉梦人生
    2020-12-30 11:23

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

    0 讨论(0)
提交回复
热议问题