c#计时器,按button1开始计时,按button2计时结束并用messagebox显示计时时间,非常感谢!!
2个回答
展开全部
简单的实现的话,根本不必用Timer的
首先定义两个datetime全局的型缓变量 DateTime startTime = new DateTime();
DateTime endTime = new DateTime();
在button1点击事件里面
private void button1_Click(object sender, EventArgs e)
{
startTime = DateTime.Now;
MessageBox.Show("碧尘计时开始!");
}
private void button2_Click(object sender, EventArgs e)
{
endTime = DateTime.Now;
MessageBox.Show((endTime-startTime).ToString());
}
这样悔租禅就可以了啊。
首先定义两个datetime全局的型缓变量 DateTime startTime = new DateTime();
DateTime endTime = new DateTime();
在button1点击事件里面
private void button1_Click(object sender, EventArgs e)
{
startTime = DateTime.Now;
MessageBox.Show("碧尘计时开始!");
}
private void button2_Click(object sender, EventArgs e)
{
endTime = DateTime.Now;
MessageBox.Show((endTime-startTime).ToString());
}
这样悔租禅就可以了啊。
追问
这个方法很好 时间单位要精确到秒怎么办
追答
DateTime.Now.ToString("hh:mm:ss");
这样就行了
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Thread MyThread;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void JiShi()
{
while (true)
{
label1.Text = DateTime.Now.ToString();
Thread.Sleep(1000);
}
}
private void button2_Click(object sender, EventArgs e)
{
MyThread.Abort();
}
private void button1_Click(object sender, EventArgs e)
{
MyThread = new Thread(new ThreadStart(JiShi));
MyThread.Start();
CheckForIllegalCrossThreadCalls = false;
}
}
}
注意引衡隐高用,在上班携洞,没办法帮你咐尺调试,请见谅
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Thread MyThread;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void JiShi()
{
while (true)
{
label1.Text = DateTime.Now.ToString();
Thread.Sleep(1000);
}
}
private void button2_Click(object sender, EventArgs e)
{
MyThread.Abort();
}
private void button1_Click(object sender, EventArgs e)
{
MyThread = new Thread(new ThreadStart(JiShi));
MyThread.Start();
CheckForIllegalCrossThreadCalls = false;
}
}
}
注意引衡隐高用,在上班携洞,没办法帮你咐尺调试,请见谅
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询