new*_*Cpp 8 c# label timer custom-controls
我创建了一个BlinkingLabel类,派生自Forms.Label,它有一个Forms.Timer允许我启用和禁用闪烁效果的类.
我创建了4个BlinkingLabel类型的标签,我的问题是如果所有4个标签在不同时间闪烁,则闪烁效果不会同步.
如何以一种方式调整我的设计,即使标签在不同时间闪烁,闪烁也会同步?
*******编辑******我添加了以下代码,但仍然无法让标签1和2同时闪烁.我要做的是测试以下内容:make label1闪烁然后我点击按钮使标签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();
}
Run Code Online (Sandbox Code Playgroud)
Han*_*ant 12
您只需使用一个计时器并使其闪烁所有标签.最优雅的方法是创建一个实现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>();
}
Run Code Online (Sandbox Code Playgroud)