How to implement a blinking label on a form

73,214

Solution 1

You can create a custom component and events to start blinking --which I think is a good solution. The Blinking you can implement with a timer.

Solution 2

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;
    }
}

Solution 3

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);

Solution 4

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.

Solution 5

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.

Share:
73,214
garik
Author by

garik

Updated on June 27, 2020

Comments

  • garik
    garik about 4 years

    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.

  • MusiGenesis
    MusiGenesis over 13 years
    -1 because downvotes are a good way to learn to avoid grisly hacks. :)
  • vlad
    vlad over 13 years
    @MusiGenesis that's personal opinion :P
  • Security Hound
    Security Hound over 13 years
    Share by multiple people, hacks are hacks, and should only be used as a last resort.
  • vlad
    vlad over 13 years
    @Ramhoud I'm guessing you didn't understand what I said. I was arguing that my solution was not a hack. It correctly solves the problem of "bringing attention to this part of the screen".
  • Baz Guvenkaya
    Baz Guvenkaya over 7 years
    What's your approach killing this process?
  • IdontCareAboutReputationPoints
    IdontCareAboutReputationPoints over 7 years
    you can add a check for a bool var inside the loop _continueBlink and check while it's true continue, else leave the loop, you can then set it to false somewhere else to leave blinking loop
  • Harold_Finch
    Harold_Finch almost 7 years
    This has been my preferred solution.
  • Rodri
    Rodri over 6 years
    Could you provide this example for NET 1.1? I would like to put it in a legacy project. Also is this working for any foreground and backgound color?
  • prototype0815
    prototype0815 over 6 years
    I use NET 3.5, and instead of async/await this solution stackoverflow.com/a/42587193/3118667 but it's not working very well
  • prototype0815
    prototype0815 over 6 years
    No I got it ! It works with WaitNMilliseconds(), not WaitNSeconds().