Search code examples
c#labelblink

C# Blink Label Class


I am trying to create a class that makes a particular textbox blink. My code is below:

class Blink
{
    int BlinkCount = 0;

    public void Text(Label Info, string Message)
    {
        Timer tmrBlink = new Timer();
        tmrBlink.Interval = 250;
        tmrBlink.Tick += new System.EventHandler(tmrBlink_Tick);
        tmrBlink.Start();
        Info.Text = Message;
    }

    private void tmrBlink_Tick(object sender, EventArgs e)
    {
        BlinkCount++;

        if (Info.BackColor == System.Drawing.Color.Khaki)
        {
            Info.BackColor = System.Drawing.Color.Transparent;
        }
        else
        {
            Info.BackColor = System.Drawing.Color.Khaki;
        }

        if (BlinkCount == 4)
        {
            tmrBlink.Stop();
        }
    }
}

The idea is, if I type the following code, the selected label would blink to draw user's attention:

Blink.Text(lblControl, "Hello World!"); 

Solution

  • This works fine for me...

    Blink b = new Blink();
    b.Text(label1, "Hello World");
    
    
    class Blink
    {
        int BlinkCount = 0;
        private Label _info;
        private Timer _tmrBlink;
    
        public void Text(Label info, string message)
        {
            _info = info;
            _info.Text = message;
            _tmrBlink = new Timer();
            _tmrBlink.Interval = 250;
            _tmrBlink.Tick += new System.EventHandler(tmrBlink_Tick);
            _tmrBlink.Start();
        }
    
        private void tmrBlink_Tick(object sender, EventArgs e)
        {
            BlinkCount++;
    
            if (_info.BackColor == System.Drawing.Color.Khaki)
            {
                _info.BackColor = System.Drawing.Color.Transparent;
            }
            else
            {
                _info.BackColor = System.Drawing.Color.Khaki;
            }
    
            if (BlinkCount == 4)
            {
                _tmrBlink.Stop();
                BlinkCount = 0;
            }
        }
    }