110,539
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 倒计时
{
public partial class Form1 : Form
{
TimeSpan ts = new TimeSpan(0, 2, 0); //声明时间段,结构为TimeSpan(hour,minute,second)
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e) //表示在设定的时间间隔后,自动触发的事件
{
ts = ts.Subtract(new TimeSpan(0, 0, 1)); //从中减去另一个TimeSpan值,每隔一秒减去一秒
string str_hr = ts.Hours > 9 ? Convert.ToString(ts.Hours) : "0" + Convert.ToString(ts.Hours);
string str_min = ts.Minutes > 9 ? Convert.ToString(ts.Minutes) : "0" + Convert.ToString(ts.Minutes);
string str_sec = ts.Seconds > 9 ? Convert.ToString(ts.Seconds) : "0" + Convert.ToString(ts.Seconds);
label1.Text = str_hr + ":" + str_min + ":" + str_sec;
if (ts.TotalSeconds <= 0.0)//当倒计时完毕
{
timer1.Enabled = false; //其中可自行添加相应的提示框或者方法函数
}
}
private void Form1_Shown(object sender, EventArgs e) //只有在首次显示窗体时才会引发 Shown 事件
{
this.WindowState = FormWindowState.Maximized; //最大化
int xWidth = SystemInformation.PrimaryMonitorSize.Width; //获取显示器屏幕宽度
int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度
label1.Font = new System.Drawing.Font("", xWidth/6);
this.button1.Location= new System.Drawing.Point(100,yHeight - 100);
this.button2.Location = new System.Drawing.Point(500, yHeight - 100);
this.button3.Location = new System.Drawing.Point(800, yHeight - 100);
}
private void button2_Click(object sender, EventArgs e) //暂停按钮
{
if (timer1.Enabled)
{
timer1.Stop();
timer1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e) //开始按钮
{
timer1.Interval = 1000;//设置每次间隔1s
timer1.Enabled = true;
timer1.Start();
}
private void button3_Click(object sender, EventArgs e) //重置按钮
{
timer1.Stop();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 倒计时
{
public partial class Form1 : Form
{
TimeSpan ts = new TimeSpan(0, 1, 0); //声明时间段,结构为TimeSpan(hour,minute,second)
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;//设置每次间隔1s
show();
}
private void show()
{
string str_hr = ts.Hours > 9 ? Convert.ToString(ts.Hours) : "0" + Convert.ToString(ts.Hours);
string str_min = ts.Minutes > 9 ? Convert.ToString(ts.Minutes) : "0" + Convert.ToString(ts.Minutes);
string str_sec = ts.Seconds > 9 ? Convert.ToString(ts.Seconds) : "0" + Convert.ToString(ts.Seconds);
label1.Text = str_hr + ":" + str_min + ":" + str_sec;
}
private void timer1_Tick(object sender, EventArgs e) //表示在设定的时间间隔后,自动触发的事件
{
ts = ts.Subtract(new TimeSpan(0, 0, 1)); //从中减去另一个TimeSpan值,每隔一秒减去一秒
show();
if (ts.TotalSeconds <= 0.0)//当倒计时完毕
{
timer1.Enabled = false; //其中可自行添加相应的提示框或者方法函数
label1.Text = "时间到!";
}
}
private void Form1_Shown(object sender, EventArgs e) //只有在首次显示窗体时才会引发 Shown 事件
{
this.WindowState = FormWindowState.Maximized; //最大化
int xWidth = SystemInformation.PrimaryMonitorSize.Width; //获取显示器屏幕宽度
int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度
label1.Font = new System.Drawing.Font("", xWidth/6);
this.button1.Location= new System.Drawing.Point(100,yHeight - 100);
this.button2.Location = new System.Drawing.Point(500, yHeight - 100);
this.button3.Location = new System.Drawing.Point(800, yHeight - 100);
}
private void button2_Click(object sender, EventArgs e) //暂停按钮
{
if (timer1.Enabled)
{
timer1.Stop();
timer1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e) //开始按钮
{
timer1.Enabled = true;
timer1.Start();
}
private void button3_Click(object sender, EventArgs e) //重置按钮
{
timer1.Stop();
ts = new TimeSpan(0, 1, 0);
show();
}
}
}