在 Windows 窗体对话框中,我有一个Listview
包含近 2000 行的对话框。每行都有一个复选框。为了方便用户,我Labels
在 Listview 上方有两个显示任何活动过滤器以及选择了多少行(选中)。
为了强调何时更改过滤器或选择,我想让标签闪烁。因此,我扩展了Label
该类以在文本更改时自动支持闪烁。但它不工作!
当我使用Windows.Forms.Timer()
标签时,它可能会闪烁一两次,但并非总是如此,而且通常它最终会被隐藏起来。
当我使用时System.Timers.Timer()
,crossthreadException
即使我使用InvokeRequired
.
怎么了 :(
class BlinkLabel : Label
{
private int _blinkFrequency = 621;
private int _maxNumberOfBlinks = 15;
private int _blinkCount = 20;
private bool _isBlinking = false;
//System.Timers.Timer _timer = new System.Timers.Timer();
Timer _timer = new Timer();
public BlinkLabel(){}
public BlinkLabel(int frequency, int maxNrBlinks)
{
_blinkFrequency = frequency;
_maxNumberOfBlinks = 15;
}
protected override void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
if (!_isBlinking)
{
base.ForeColor = System.Drawing.Color.Purple;
StartBlink();
}
}
public void StartBlink()
{
this._isBlinking = true;
base.Visible = true;
this._timer.Interval = this._blinkFrequency;
this._timer.Enabled = true;
//this._timer.AutoReset = true;
//this._timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
this._timer.Tick += new EventHandler(_timer_Tick);
this._timer.Start();
}
void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//SYSTEM.TIMERS.TIMER() TICK
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => { this._timer_Elapsed(sender, e); }));
}
else
{
base.Visible = !base.Visible;
this._blinkCount++;
Application.DoEvents();
if (this._blinkCount >= this._maxNumberOfBlinks)
{
this._timer.Stop();
this._blinkCount = 0;
base.Visible = true;
this._isBlinking = false;
}
}
}
void _timer_Tick(object sender, EventArgs e)
{
//WINDOWS.FORMS.TIMER TICK
this.Visible = !this.Visible;
this._blinkCount++;
Application.DoEvents();
if (this._blinkCount >= this._maxNumberOfBlinks)
{
this._timer.Stop();
this._blinkCount = 0;
//base.Visible = true;
this.Visible = true;
this._isBlinking = false;
}
}
}