C#窗体设计:[31]Timer倒计时
实现Timer倒计时
操作方法
- 01
新建一个C#应用程序,应用程序命名为Countdown。
- 02
窗体界面设计如下:
- 03
双击窗体添加响应函数Form1_Load(),响应函数Form1_Load()中添加一下代码: private 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();//计时器开始运行 }
- 04
双击Timer控件为其添加响应函数timer1_Tick(),并添加一下代码。 private void timer1_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 + "天"; }
- 05
程序运行如下:
赞 (0)