我在做一个小的应用程序的时候,想添加一个Timer控件来标明程序运行的时间,我和好友也没商量出结果,手头的资料也不能解决,只好拜托各位拉!
有关控件的应用我们讲 的很少,我主要想建一个计时器,帮帮忙把!
long timeStart;
TimeSpan timeSpan;
private void timer1_Tick(object sender, System.EventArgs e)
{
long current=System.Environment.TickCount;
timeSpan=new TimeSpan (current*10*1000);
this.label1 .Text ="本计算机启动时间:"+timeSpan.ToString ();
timeSpan=new TimeSpan((current-timeStart)*10*1000);
this.label2.Text ="程序运行时间:"+timeSpan.ToString ();
string aa=Convert.ToString(DateTime.Today);//DateTime.Today.ToString() 显示的是年月日时分秒
string[] asd=aa.Split(' ');\\使它只显示年月日
this.label3.Text ="当前时间:"+asd[0];
}
private void Form1_Load(object sender, System.EventArgs e)
{
timeStart=System.Environment.TickCount;
this.timer1 .Enabled =true;
this.timer1 .Interval =1000;
}
你看看这段代码吧 或许有帮助
public partial class frmTime : Form
{
int count = 0;
public frmTime ()
{
InitializeComponent();
count = Environment.TickCount;
}
/// <summary>
/// 每秒将做的事情
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
//获得机器的运行时长
int current = Environment.TickCount;
int second = current / 1000 % 60;
int minute = current / (1000 * 60) % 60;
int hour = current / (1000 * 60 * 60) ;
label3.Text = string.Format("{0}:{1}:{2}",hour,minute,second);
//获得程序运行时间
int dif = count-current ;
second = dif / 1000 % 60;
minute = dif / (1000 * 60) % 60;
hour = dif / (1000 * 60 * 60);
label4.Text = string.Format("{0}:{1}:{2}", hour, minute, second);
}
[此贴子已经被作者于2006-5-22 20:59:11编辑过]