Sync Blinking Labels in C#

霸气de小男生 提交于 2020-01-03 08:45:07

问题


I created a BlinkingLabelclass, derives from Forms.Label, which has a Forms.Timer which allows me to enable and disable the blinking effect.

I have created 4 labels of BlinkingLabel type, my problem is that if all 4 labels where to blink in different times, the blinking effect is not synced.

How can I adjust my design in a way that even if the labels where blinking in different times, the blinking will be synced?

******* Edited****** I added the following code, but still I can't get label 1 and 2 to blink same time. What am trying to do to is to test the following: make label1 blink then I click button to make label 2 to blink and they are not synced.

Any idea what am doing wrong?

public partial class UserControl1 : UserControl
{
    Timer blinkTimer;
    Color blinkingColor = Color.Red;
    int interval = 300;

    bool flag1 = false;
    bool flag2 = false;

    public UserControl1()
    {
        InitializeComponent();      // Blinking abel default values
        this.blinkTimer = new Timer();
        this.blinkTimer.Interval = interval; ;
        this.blinkTimer.Tick += new System.EventHandler(timer_Tick);
        flag1 = true;
        this.blinkTimer.Start();
    }

    private void blinkLabels(Label label)
    {
        if (label.ForeColor == Color.White)
            label.ForeColor = blinkingColor;
        else
            label.ForeColor = Color.White;
    }

    void timer_Tick(object sender, System.EventArgs e)
    {
        if(flag1 == true)
            blinkLabels(label1);
        if(flag2 == true)
            blinkLabels(label2);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        flag2 = true;
        this.blinkTimer.Start();

    }

回答1:


You need to use just one Timer and get it to blink all the labels. This is most elegantly done by creating a component that implements the IExtenderProvider interface. Similar to the way ErrorProvider works, I'll show you one that adds a Blink property to every Label control on a form.

Add a new class to your project and paste the code shown below. Compile. Drop the new component from the top of the toolbox onto your form. Set the Blink property to any label that needs to blink to True.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

[ProvideProperty("Blink", typeof(Label))]
public class BlinkProvider : Component, IExtenderProvider {
    public BlinkProvider() {
        timer = new Timer();
        timer.Tick += timer_Tick;
        Interval = 500;
        Enabled = true;
        BlinkingColor = Color.Red;
    }
    public BlinkProvider(IContainer container)
        : this() {
        container.Add(this);
    }

    protected override void Dispose(bool disposing) {
        if (disposing) timer.Dispose();
        base.Dispose(disposing);
    }

    [DefaultValue(500)]
    public int Interval {
        get { return timer.Interval; }
        set { timer.Interval = value; }
    }

    [DefaultValue(true)]
    public bool Enabled { get; set; }

    [DefaultValue(typeof(Color), "255, 255, 0, 0")]
    public Color BlinkingColor {
        get;
        set;
    }

    private void timer_Tick(object sender, EventArgs e) {
        if (this.DesignMode) return;
        tock = !tock;
        foreach (var lbl in labels.Keys) {
            if (labels[lbl].Blink && Enabled && tock) lbl.ForeColor = BlinkingColor;
            else lbl.ForeColor = labels[lbl].ForeColor;
        }
    }

    bool IExtenderProvider.CanExtend(object extendee) {
        return extendee is Label;
    }
    public bool GetBlink(Label label) {
        AddLabelIfNecessary(label);
        return labels[label].Blink;
    }
    public void SetBlink(Label label, bool blink) {
        AddLabelIfNecessary(label);
        labels[label].Blink = blink;
    }
    private void AddLabelIfNecessary(Label label) {
        if (!labels.ContainsKey(label)) {
            labels.Add(label, new BlinkInfo { Blink = false, ForeColor = label.ForeColor });
        }
        timer.Enabled = !DesignMode;
    }

    private Timer timer;
    private bool tock;
    private class BlinkInfo {
        public Color ForeColor;
        public bool Blink;
    }
    private Dictionary<Label, BlinkInfo> labels = new Dictionary<Label, BlinkInfo>();
}



回答2:


they do change color at the same time dont they but they aren't synced. to sync them and use the same color. You have to change the code so that the blink color is kept by the timer or the control not the labels try something like this

 Color currentBlink;     
 private void blinkLabels(Label label)
 {
            label.ForeColor = currentBlink;        
 }

 void timer_Tick(object sender, System.EventArgs e)
    {
        if (currentBlink== Color.White)
            currentBlink = blinkingColor;
        else
            currentBlink = Color.White;


        if(flag1 == true)
            blinkLabels(label1);
        if(flag2 == true)
            blinkLabels(label2);
    }


来源:https://stackoverflow.com/questions/30095333/sync-blinking-labels-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!