How to implement a blinking label on a form

2019-02-08 13:41发布

I have a form that displays queue of messages and number this messages can be changed. Really I want to blink label (queue length) when the number of messages were increased to improve form usability. Should I implement custom control and use additional thread or timer to change color of label? Has anybody implemented so functionality? What is the best solution (less resources and less performance degradation) to implement so behaviour?

SOLUTION:

Form's component with timer that can restrict number of animations per second and implement fade out effect to external control background color.

7条回答
淡お忘
2楼-- · 2019-02-08 13:43

The following is blinking using async and await

private async void Blink(){
    while (true){
        await Task.Delay(500);
        label1.BackColor = label1.BackColor == Color.Red ? Color.Green : Color.Red;
    }
}
查看更多
倾城 Initia
3楼-- · 2019-02-08 13:50
Timer timer = new Timer();
timer.Interval = 500;
timer.Enabled = false;

timer.Start();

if( messagesNum > oldMessagesNum)
  timer.Tick += new EventHandler( timer_Tick );
else
  timer.Tick -= timer_Tick;

void timer_Tick( object sender, EventArgs e )
{
   if(messageLabel.BackColor == Color.Black)
      messageLabel.BackColor = Color.Red;
   else
      messageLabel.BackColor = Color.Black;
}

Here is a pretty simple implementation that would work inside your form. You could also create a custom control with the same code and just throw the Timer.Start() into a method for that control.

查看更多
看我几分像从前
4楼-- · 2019-02-08 13:50

You can use Timer class here. Here what I have implemented. Label color blinking on Button_click Event.

//click event on the button to change the color of the label
public void buttonColor_Click(object sender, EventArgs e)
        {
            Timer timer = new Timer();
            timer.Interval = 500;// Timer with 500 milliseconds
            timer.Enabled = false;

            timer.Start();

            timer.Tick += new EventHandler(timer_Tick);
        }

       void timer_Tick(object sender, EventArgs e)
    {
        //label text changes from 'Not Connected' to 'Verifying'
        if (labelFirst.BackColor == Color.Red)
        {
            labelFirst.BackColor = Color.Green;
            labelFirst.Text = "Verifying";
        }

        //label text changes from 'Verifying' to 'Connected'
        else if (labelFirst.BackColor == Color.Green)
        {
            labelFirst.BackColor = Color.Green;
            labelFirst.Text = "Connected";
        }

        //initial Condition (will execute)
        else
        {
            labelFirst.BackColor = Color.Red;
            labelFirst.Text = "Not Connected";
        }
    }
查看更多
We Are One
5楼-- · 2019-02-08 13:51

You can create custom component and events for start blinking. I think is good solution. Blinking you can implement with timer.

查看更多
甜甜的少女心
6楼-- · 2019-02-08 13:52

Create your own UserControl for this, one that inherits from Label instead of from Control directly. Add a StartBlinking method, in which you start a Timer object whose tick event alters the style of the label (changing the BackgroundColor and ForegroundColor properties each time to create the blink effect).

You could also add a StopBlinking method to turn it off, or you could have your Timer stop itself after 5 seconds, perhaps.

查看更多
Explosion°爆炸
7楼-- · 2019-02-08 13:56

I know this is a really old post, but anyone looking for something a little more versatile than the Boolean solutions posted may get some use out of the following: enter image description here

using System.Diagnostics;
using System.Threading.Tasks;

private async void SoftBlink(Control ctrl, Color c1, Color c2, short CycleTime_ms, bool BkClr)
{
    var sw = new Stopwatch(); sw.Start();
    short halfCycle = (short)Math.Round(CycleTime_ms * 0.5);
    while (true)
    {
        await Task.Delay(1);
        var n = sw.ElapsedMilliseconds % CycleTime_ms;
        var per = (double)Math.Abs(n - halfCycle) / halfCycle;
        var red = (short)Math.Round((c2.R - c1.R) * per) + c1.R;
        var grn = (short)Math.Round((c2.G - c1.G) * per) + c1.G;
        var blw = (short)Math.Round((c2.B - c1.B) * per) + c1.B;
        var clr = Color.FromArgb(red, grn, blw);
        if (BkClr) ctrl.BackColor = clr; else ctrl.ForeColor = clr;
    }
}

Which you can call like such:

SoftBlink(lblWarning, Color.FromArgb(30, 30, 30), Color.Red,2000,false);
SoftBlink(lblSoftBlink, Color.FromArgb(30, 30, 30), Color.Green, 2000,true);
查看更多
登录 后发表回答