在C#中同步闪烁标签 [英] Sync Blinking Labels in C#

查看:132
本文介绍了在C#中同步闪烁标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个BlinkingLabel类,该类是从Forms.Label派生的,该类具有一个Forms.Timer,该类允许我启用和禁用闪烁效果.

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

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

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?

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

******* 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.

有什么想法吗?

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

    }

推荐答案

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

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.

向您的项目添加一个新类,并粘贴以下代码.编译.将新组件从工具箱的顶部拖放到您的窗体上.将Blink属性设置为任何需要闪烁为True的标签.

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

这篇关于在C#中同步闪烁标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆