我创建了一个BlinkingLabel
类,源自Forms.Label
,其中有一个Forms.Timer
这允许我启用和禁用闪烁效果。
我创建了 4 个标签BlinkingLabel
类型,我的问题是,如果所有 4 个标签在不同时间闪烁,则闪烁效果不同步。
我如何调整我的设计,即使标签在不同时间闪烁,闪烁也会同步?
***** 已编辑 *****
我添加了以下代码,但仍然无法让标签 1 和 2 同时闪烁。我想做的是测试以下内容:使标签 1 闪烁,然后我单击按钮使标签 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();
}
你只需要使用one计时器并使其闪烁all标签。通过创建一个实现 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>();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)