【发布时间】:2016-09-28 14:09:39
【问题描述】:
我创建了一个包含计时器的小类 CountDown 。
类很简单:接收时间目标,并使用计时器启动倒计时。
当达到目标时,我的个人事件触发:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CountDownWithEvent
{
public delegate void countDownFinishEventHandler(Object sender, EventArgs e);
class CountDown
{
private DateTime _target;
private System.Windows.Forms.Timer _timer;
System.TimeSpan _timeMissing;
public event countDownFinishEventHandler CountDownFinish;
public CountDown(DateTime targetTime)
{
this._target = targetTime;
this._timer = new System.Windows.Forms.Timer();
this._timeMissing = new TimeSpan();
}
public DateTime TargetTime
{
get;
}
public TimeSpan timeMissing
{
get;
}
public void CountDownStart()
{
_timer.Interval = 1000;
_timer.Tick += new EventHandler(timer_tick);
_timer.Start();
}
protected virtual void timer_tick(Object sender, EventArgs e)
{
//if (_timer.Tick != null)
//{
//}
System.DateTime now = System.DateTime.Now;
_timeMissing = _target.Subtract(now);
if (!(timeMissing.TotalSeconds > 0))
{
_timer.Stop();
if(CountDownFinish != null)
{
EventArgs b = new EventArgs();
CountDownFinish(this, b);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CountDownWithEvent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CountDown CountDown = new CountDown(new DateTime(2016, 05, 27, 14, 48, 00));
CountDown.CountDownFinish += new countDownFinishEventHandler(onCountDown);
CountDown.CountDownStart();
}
private void onCountDown(Object sender, EventArgs e)
{
MessageBox.Show("time expired! ");
}
}
}
我使用EventArgs,而不是写派生类,因为我不需要事件的任何特殊信息(理解,参数e)
现在我的情况有点不寻常:
protected virtual void timer_tick(Object sender, EventArgs e)
{
//if (_timer.Tick != null)
//{
//}
System.DateTime now = System.DateTime.Now;
_timeMissing = _target.Subtract(now);
if (!(timeMissing.TotalSeconds > 0))
{
_timer.Stop();
if(CountDownFinish != null)
{
EventArgs b = new EventArgs();
CountDownFinish(this, b);
}
}
}
当我调用CountdownFinish(this,e); 时,e 参数指的是 timer_tick
通过 EventArgs 计时器不一致
所以我不知道该怎么做???
其实我已经实例化了新的EventArgs b
EventArgs b = new EventArgs();
CountDownFinish(this, b);
但我不知道这是否是正确的道路
现在我是另一个问题:
我想在标签中看到距离目标还剩多少时间。并将其刷新到任何 timer_tick。 而我想保持程序图形的定时器逻辑分开.. 我该怎么做?
非常感谢您的理解和帮助! (抱歉英语不好,不是我的语言:))
【问题讨论】:
-
如果您解决了问题,请不要在同一个帖子中添加另一个问题,而是提出新问题。
-
谢谢提问..但我还没有解决...第一个是疑问..第二个是问题
-
1- 要提供一个值以用于没有事件数据的事件,请使用
EventArgs.Empty。 2- 创建TimeLeft属性,该属性显示完成的剩余时间并在计时器滴答事件中减少它。同时创建TimeLeftChanged事件,当TimeLeft改变时触发。 -
@RezaAghaei 谢谢。如果我创建一个减少的属性......如何刷新标签?!这是我想念的段落(我希望克服我的困难)..而
CountDown和Form是一个单独的类...Also create TimeLeftChanged event which fires when TimeLeft changes使用此解决方案如何将数据传递给标签?在CountDown类型标签中创建一个成员类? -
在表单中订阅组件的
TimeLeftChanged事件,并在处理事件的方法中将标签文本设置为组件TimeLeft属性的值。
标签: c# winforms events timer event-handling