Mogation and change the colour of the button using different time intervals



  • I want to make a button. Сохранитьin order to successfully perform the operation that it caused the following effect: it gradually blured into green colour, say every 0.6 seconds it blinks green, 2 times, then every 0.4 seconds, 2 times, then 0.2 seconds, 2 times, more and more, at the end it becomes completely green and it does not change. About 1 second, then back in his standard color. There is also a retroactive effect on this principle with red colour. If the flowers are clear, the timers are in trouble, please help. C# programme in VS 2015, WinFormApplication. I've seen DispatcherTimer() But it didn't even work out, because I can't connect. using System.Windows.Threading; I swear.



  • I'm not a WinForms specialist, but specifically what you want can be done:

    async Task PlaySuccessAnimation(Button b, CancellationToken ct)
    {
        // проверка
        if (b.UseVisualStyleBackColor == true)
            throw new InvalidOperationException("Need overridable color");
    
    var origColor = b.BackColor;
    
    var on = TimeSpan.FromSeconds(0.1);
    try
    {
        // два раза по 0.6 секунд мигаем зелёным
        await Blink(b, Color.Green, on, TimeSpan.FromSeconds(0.5), 2, ct);
    
        // 2 раза по 0.4 секунды
        await Blink(b, Color.Green, on, TimeSpan.FromSeconds(0.3), 2, ct);
    
        // 2 раза по 0.2 секунды
        await Blink(b, Color.Green, on, TimeSpan.FromSeconds(0.1), 2, ct);
    
        b.BackColor = Color.Green;
    }
    catch (OperationCanceledException ex)
    {
        b.BackColor = origColor;
        throw;
    }
    

    }

    async Task Blink(
    Button b, Color color,
    TimeSpan on, TimeSpan off, int n,
    CancellationToken ct)
    {
    var origColor = b.BackColor;
    for (int i = 0; i < n; i++)
    {
    b.BackColor = Color.Green;
    await Task.Delay(on, ct);
    b.BackColor = origColor;
    await Task.Delay(off, ct);
    }
    }

    I didn't try, but I don't think it's gonna look good. Wake up!


    If you're not going to stop the animation, you can just use it. CancellationToken.None as ct♪ If you're going, write like,

    var cts = new CancellationTokenSource();
    // сохраните cts где-то для дальнейшего использования
    await PlaySuccessAnimation(b, cts.Token);

    If you need to interrupt the animation, use it. cts.Cancel()




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2