【问题标题】:Sync Blinking Labels in C#在 C# 中同步闪烁标签
【发布时间】:2015-05-07 08:10:24
【问题描述】:

我创建了一个BlinkingLabelclass,派生自Forms.Label,它有一个Forms.Timer,允许我启用和禁用闪烁效果。

我已经创建了 4 个BlinkingLabel 类型的标签,我的问题是如果所有 4 个标签在不同时间闪烁,闪烁效果不同步。

如何调整我的设计,即使标签在不同时间闪烁,闪烁也会同步?

************已编辑****** 我添加了以下代码,但我仍然无法让标签 1 和 2 同时闪烁。我想要做的是测试以下内容:让 label1 闪烁然后我单击按钮使 label 2 闪烁并且它们不同步。

知道做错了什么吗?

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

    }

【问题讨论】:

  • 对所有标签使用一个计时器。您可以在标签中进行覆盖,以允许您控制可以查看/显示所有标签的类的闪烁。然后使用该类中的计时器让它们全部闪烁。
  • ..您仍然可以在每个标签上设置一个标志,以使其参与庆祝活动与否..
  • @deathismyfriend 请看看我的测试代码。谢谢。

标签: c# timer label custom-controls


【解决方案1】:

您只需要使用一个计时器并让它闪烁所有标签。这是通过创建一个实现 IExtenderProvider 接口的组件来最优雅地完成的。与 ErrorProvider 的工作方式类似,我将向您展示一个为表单上的每个 Label 控件添加 Blink 属性的方法。

向您的项目添加一个新类并粘贴如下所示的代码。编译。将新组件从工具箱顶部拖放到表单上。将 Blink 属性设置为任何需要闪烁为 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】:

    它们确实会同时改变颜色,但它们没有同步。 同步它们并使用相同的颜色。您必须更改代码,以便计时器或控件保持闪烁颜色而不是标签 试试这样的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 2014-02-20
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 2015-06-23
      相关资源
      最近更新 更多