0

I got a problem.I am giving colors to the text in Rich text box.When the timer is on ,all the text was turning to aqua color.But I need Different colors.Here is my code

    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;
    }
Joe Korolewicz
  • 474
  • 4
  • 21
suhas
  • 11
  • 7

1 Answers1

1

This line:

richTextBox1.Text = str1;

replaces all of the existing formatting.

You have to select the range of characters and then replaces it with a valid RTF string.

I suspect you want your tick event to look like this (no error checking):

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;
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Well Thank you for your reply.But the abvoe code did not work.I text is flickering.please check again – suhas Jul 19 '13 at 18:04
  • @user2569839 Define "did not work" please. In my test, my code did exactly what your example did, except it kept the color formatting. Yes, flickering is a by-product of manipulating the RTB control. See this [flicker free RichTextBox control](http://stackoverflow.com/a/6550415/719186) to fix that problem. – LarsTech Jul 19 '13 at 18:22
  • Please make the above whole code along with my code work. Make it flicker free and post. – suhas Jul 19 '13 at 18:37
  • 1
    @user2569839 I'm not your servant. I did my best to help you. – LarsTech Jul 19 '13 at 18:38
  • K brother.sorry for asking like that.forgive me.But please help. – suhas Jul 19 '13 at 18:50
  • @user2569839 Ignore the flicker issue. What is not working? I need you to describe what you are getting versus what you are expecting? – LarsTech Jul 19 '13 at 18:55
  • The string words has to scroll from right to left end and the color of each string should be as formatted color.My problem is the text was not coming till the left end.In the middle it is flickering.And one more thing i have to change the fore color. – suhas Jul 19 '13 at 19:01
  • @user2569839 `text was not coming til the left end`. I don't know what that means. Your code is taking the first character and moving it to the end. My code is doing the same thing, but keeps the formatting. Use `richTextBox1.SelectionColor` for the fore color. – LarsTech Jul 19 '13 at 19:11
  • K.I have used that .But Text has to scroll from right to left.But text from right to middle is visible and the remaining was not visible.May be due to flickering. – suhas Jul 19 '13 at 19:22
  • In that flicker free program.getting error in this.selection and while sending message handle problem – suhas Jul 19 '13 at 19:24
  • @user2569839 The RichTextBox control wasn't made for that. It's not a marquee sign. – LarsTech Jul 19 '13 at 19:34