在窗体中添加一个Timer和一个Label
int time=10;
private void Form1_Load(object sender, System.EventArgs e)
{
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.timer1.Start();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if(time>-1)
{
time--;
this.label1.Text=time.ToString();
this.label1.Refresh();
}
else
{
timer1.Stop();
}
}
int time=10;
private void Form1_Load(object sender, System.EventArgs e)
{
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.timer1.Start();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if(time>-1)
{
time--;
this.label1.Text=time.ToString();
this.label1.Refresh();
}
else
{
timer1.Stop();
}
}
tmp--; if (tmp == 0)
{
this.label1.Text = "时间到!!";
timer.Stop();
return;
} this.label1.Text = Convert.ToString(tmp);
{
if (this.dtExam < DateTime.Now)
{
this.timerCD.Enabled = false;
this.Close();
}
else
{
RefreshTime();
}
} private void RefreshTime()
{
TimeSpan ts = this.dtExam - DateTime.Now;
this.labDays.Text = ts.Days.ToString().PadLeft(2, '0') + "天";
this.labTimes.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" +
ts.Minutes.ToString().PadLeft(2, '0') + ":" +
ts.Seconds.ToString().PadLeft(2, '0');
} private void CountDown_Load(object sender, EventArgs e)
{
if (DateTime.Now > this.dtExam)
{
this.timerCD.Enabled = false;
this.Close();
}
else
{
RefreshTime();
}
}
}timerCD 控件的 Interval 值为 1000; Enable 为 True;
怎么设置在time=0时,过一分钟再从10循环开始呢?
谢谢!
private void Form1_Load(object sender, System.EventArgs e)
{
this.label1.Text = string.empty;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.timer1.Start();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if(time>0) {
this.label1.Text=time.ToString();
time--;
}
else
{
timer1.Enabled = false;
timer1.Stop();
return;
}
} 测试过了 好用 呵呵
{
int time = 100;
DateTime date = new DateTime();
date = DateTime.Now;
int k = date.Second;
while (true)
{
date = DateTime.Now;
int m = date.Second; if (m != k)
{
Console.WriteLine(time--);
k = m;
}
} }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace random
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private int time = 60;
private void Form2_Load(object sender, EventArgs e)
{
this.timer1.Interval = 1000;
this.timer1.Tick+=new EventHandler(timer1_Tick);
timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
if (time > 0)
{
label1.Text = time.ToString();
time--;
// label1.Refresh();
}
else
{
timer1.Stop();
}
while (time == 0)
{
timer1.Stop();
time = 10;
}
}
}
}
你可以把这句代码去掉就可以一秒一秒减少
EventHandler 是一个预定义的委托,专用于表示不生成数据的事件的事件处理程序方法。如果事件生成数据,则必须提供自己的自定义事件数据类型,并且必须要么创建一个委托,其中第二个参数的类型为自定义类型,要么使用泛型 EventHandler 委托类并用自定义类型替代泛型类型参数。若要将事件与处理事件的方法关联,请向事件添加委托的实例。除非移除了该委托,否则每当发生该事件时就调用事件处理程序。 它就委托两次,所以都是两秒两秒的减少
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace daojishi
{
public partial class Form1 : Form
{
int time = 60;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (time > 0)
{
time--;
label1.Text = time.ToString ();
}
else
{
timer1.Stop();
} } private void label1_Click(object sender, EventArgs e)
{ } }
}