1、新建一个C#应用程序,应用程序命名为Countdown。
2、窗体界面设计如下:
3、双击窗体添加响应函数Form1_Load(),响应函数Form1_Load()中添加一下代艨位雅剖码:pri即枢潋雳vate void Form1_Load(object sender, EventArgs e) { txtTimeNow.Text = DateTime.Now.ToString(); txtEndTime.Text = "2015-10-01 00:00:00"; txtTimeNow.ReadOnly = true; txtEndTime.ReadOnly = true; this.timer1.Start();//计时器开始运行 }
4、双击Timer控件为其添加响应函数timer1_Tick(),并添加一下代码。private void ti罪焐芡拂mer1_Tick(object sender, EventArgs e) { DateTime timeend = Convert.ToDateTime(txtEndTime.Text);//设置结束时间 txtTimeNow.Text = DateTime.Now.ToString();//设置当前时间 string timeday = DateAndTime.DateDiff("d", DateTime.Now, timeend, FirstDayOfWeek.Sunday, FirstWeekOfYear.FirstFourDays).ToString(); this.lblTime.Text = timeday + "天"; }
5、程序运行如下: