0

我遇到了问题。我正在为富文本框中的文本提供颜色。当计时器打开时,所有文本都变成了水色。但我需要不同的颜色。这是我的代码

    private void Form1_Load_1(object sender, EventArgs e)
    {
        //richTextBox1.Font = new Font("Consolas", 18f, FontStyle.Bold);
        richTextBox1.BackColor = Color.AliceBlue;
        string[] words = { "Sachin tendulkas(40)", "Virendra sehwag(35)", "Dhoni", "Ramesh", "Saurov ganguly(39)", "Venkatesh prasad(44)" };
        Color[] colors =
               {
    Color.Aqua,
    Color.CadetBlue,
    Color.Cornsilk,
    Color.Gold,
    Color.HotPink,
    Color.Lavender,
    Color.Moccasin
    };

        for (int i = 0; i < words.Length; i++)
        {
            string word = words[i];
            Color color = colors[i];
            {
                richTextBox1.SelectionBackColor = color;
                richTextBox1.AppendText(word);
                //richTextBox1.SelectionBackColor = Color.AliceBlue;
                richTextBox1.AppendText(" ");
            }
        }
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        string str1 = richTextBox1.Text;
        str1 = str1.Substring(1) + str1.Substring(0, 1);
        richTextBox1.Text = str1;
    }
4

1 回答 1

1

这一行:

richTextBox1.Text = str1;

替换所有现有格式。

您必须选择字符范围,然后将其替换为有效的 RTF 字符串。

我怀疑您希望您的滴答事件看起来像这样(没有错误检查):

private void timer1_Tick(object sender, EventArgs e) {
  richTextBox1.Select(0, 1);
  string rtf = richTextBox1.SelectedRtf;
  richTextBox1.SelectedText = string.Empty;
  richTextBox1.Select(richTextBox1.Text.Length, 0);
  richTextBox1.SelectedRtf = rtf;
}
于 2013-07-19T16:29:09.703 回答