c#winform做的一个计时器,按下暂停按钮在开始的时候时间清零了,怎么写才能继续往上加?
privateDateTimedtBegin=DateTime.Now;privatevoidbutton1_Click(objectsender,EventArgse)...
private DateTime dtBegin = DateTime.Now;
private void button1_Click(object sender, EventArgs e) { if (this.button1.Text == "开始") { dtBegin = DateTime.Now; timer1.Start(); this.button1.Text = "暂停"; } else { dtBegin = DateTime.Now; timer1.Stop(); this.button1.Text = "开始"; } } private TimeSpan GetTimediff(DateTime dt1, DateTime dt2) { return dt2 > dt1 ? dt2 - dt1 : dt1 - dt2; }
private void timer1_Tick(object sender, EventArgs e) { TimeSpan ts = GetTimediff(dtBegin, DateTime.Now); txtHour.Text = ts.Hours.ToString(); txtMin.Text = ts.Minutes.ToString(); txtSec.Text = ts.Seconds.ToString(); }
private void button2_Click(object sender, EventArgs e) { TimeSpan ts = GetTimediff(dtBegin, DateTime.Now); MessageBox.Show(string.Format("您本次的计时时间为:{0}小时{1}分{2}秒", ts.Hours, ts.Minutes, ts.Seconds), "计时结束", MessageBoxButtons.OK, MessageBoxIcon.Information); }
这是写的代码
if (this.button1.Text == "开始")
{
if (txtSec.Text == "0")
{
dtBegin = DateTime.Now;
timer1.Start();
this.button1.Text = "暂停";}
else
{
timer1.Start();
this.button1.Text = "暂停";
}
}
else
{
timer1.Stop();
this.button1.Text = "开始";
} 展开
private void button1_Click(object sender, EventArgs e) { if (this.button1.Text == "开始") { dtBegin = DateTime.Now; timer1.Start(); this.button1.Text = "暂停"; } else { dtBegin = DateTime.Now; timer1.Stop(); this.button1.Text = "开始"; } } private TimeSpan GetTimediff(DateTime dt1, DateTime dt2) { return dt2 > dt1 ? dt2 - dt1 : dt1 - dt2; }
private void timer1_Tick(object sender, EventArgs e) { TimeSpan ts = GetTimediff(dtBegin, DateTime.Now); txtHour.Text = ts.Hours.ToString(); txtMin.Text = ts.Minutes.ToString(); txtSec.Text = ts.Seconds.ToString(); }
private void button2_Click(object sender, EventArgs e) { TimeSpan ts = GetTimediff(dtBegin, DateTime.Now); MessageBox.Show(string.Format("您本次的计时时间为:{0}小时{1}分{2}秒", ts.Hours, ts.Minutes, ts.Seconds), "计时结束", MessageBoxButtons.OK, MessageBoxIcon.Information); }
这是写的代码
if (this.button1.Text == "开始")
{
if (txtSec.Text == "0")
{
dtBegin = DateTime.Now;
timer1.Start();
this.button1.Text = "暂停";}
else
{
timer1.Start();
this.button1.Text = "暂停";
}
}
else
{
timer1.Stop();
this.button1.Text = "开始";
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询