定义计时器
全局变量private Timer Timer;
private void Form1_Load(object sender, EventArgs e)
{
Timer = new Timer();
Timer.Interval = 1000;
Timer.Tick += new EventHandler(Timer_Tick);
Timer.Start();
}
窗体上加一个label控件,名为label1;
private void Timer_Tick(object sender, EventArgs e)
{
this.label1.Text = DateTime.Now.ToString("HH:mm:ss");
}
有问题再联系
以下代码,需新建一windows form项目,并在窗体上添加一个Lable1控件。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private delegate void settime();
public Form1()
{
InitializeComponent();
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
SetTime();
}
private void SetTime()
{
if (label1.InvokeRequired)
{
settime settime = new settime(SetTime);
this.BeginInvoke(settime);
}
else
{
label1.Text = DateTime.Now.ToString();
}
}
}
}
使用timer控件
private void InitializeTimer()
{
//' Run this procedure in an appropriate event.
// Set to 1 second.
Timer1.Interval = 1000;
// Enable timer.
Timer1.Enabled = true;
Button1.Text = "Stop";
}
用时间空间,判断呗,和你要对比的时间就可以了,还有就是你定时,多少分钟后响,你就用timer控件间隔一定时间自行一次,然后停止就可以了,
加一个timer,每隔一秒刷新一次时间!