I'm making a running text software where there is some text that runs alternately in the marqueelabel control. I wanted at every turn of the text, the background of marqueelabel will be blinking.
I use three timer control, ie two System.Windows.Forms.Timer (Timer1 and timer2) and one System.Timers.Timer (elapsedTime). Timer1 is used to replace the text that goes in a certain duration. timer2 desired for the blink The text for 2 seconds set by elapsedTime. all went well, except the text does not blink every turn of the text as i expected.
Here is the code :

using System; 
using System.Timers; 
using System.Threading; 
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace RunningText
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        private Dictionary<int,textData> dict = new Dictionary<int,textData>();
        private string[] textContent;
        private string[] textDuration;
        private int      textCount  = 0;
        private int      textIdx    = 0;
        private System.Timers.Timer elapsedTime;

    struct textData
    {
        public string content;
        public int duration;//in second
    }

    public MainForm()
    {
        InitializeComponent();

        fillData();

        timer1.Start();
        loadText();
    }

    public void fillData()
    {
        textData td;
        td.content = "Love means never having to say you're sorry";
        td.duration = 15;
        dict.Add(0,td);

        td.content = "A long time ago, in a galaxy far, far away...";
        td.duration = 20;
        dict.Add(1,td);

        td.content = "Just when you thought it was safe to go back in the water...";
        td.duration = 15;
        dict.Add(2,td);
    }

    public void loadText()
    {
        textCount = dict.Count;
        if(textCount > 0)
        {
            textContent     = new string[textCount];
            textDuration    = new string[textCount];

            for(int i=0;i<textCount;i++)
            {
                textContent[i] = dict[i].content;
                textDuration[i] = dict[i].duration.ToString();
            }
            textIdx = 0;
            showText(textContent[textIdx],textDuration[textIdx]);           

        }
    }

    private void showText(string content,string duration)
    {
        timer1.Stop();
        timer2.Stop();

        marqueeLabel1.Text = content;

        int second = Convert.ToInt32(duration) * 1000;
        timer1.Interval = second;

        timer1.Start();

        elapsedTime = new System.Timers.Timer(10000);
        elapsedTime.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
        elapsedTime.Interval = 2000;
        elapsedTime.AutoReset = true;
        elapsedTime.Start();

        timer2.Tick += blinkLabel; 
        timer2.Interval = 200; 
        timer2.Start();
    }

    void Timer1Tick(object sender, EventArgs e)
    {
        if(textCount > 0)
        {
            textIdx++;
            if(textCount == textIdx)
            {
                textIdx = 0;
            }
            showText(textContent[textIdx],textDuration[textIdx]);
        }
    }
    private void blinkLabel(object sender, EventArgs e) 
    {
        if (marqueeLabel1.BackColor == Color.White) 
            marqueeLabel1.BackColor = Color.Gold;
        else 
            marqueeLabel1.BackColor = Color.White;
    }

    private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        timer2.Stop();
        elapsedTime.Stop();
        marqueeLabel1.BackColor = Color.White;
    }
}

the complete file and the marqueelabel control can be found here : http://www.ziddu.com/download/21091002/RunningText.zip.html

Well, this is perhaps not exactly what you want to achieve, but it might give you some ideas.

thanks for the reply ddanbe. Yeah, i dont want to change my code much. I think the culprit are the timers. But i dont have idea.

try calling blinkLabel from the timer2 tick event. blinkLabel isn't returning a value so I can't see timer2.Tick += blinkLabel; working

Thanks tinstaafl, its working now as i expected. here is some changes :

private void showText(string content,string duration)
{
    ...
    elapsedTime.Start();

    //timer2.Tick += blinkLabel; 
    timer2.Interval = 200; 
    timer2.Start();
}

and

void Timer2Tick(object sender, EventArgs e)
{
    blinkLabel(sender,e);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.