pojowsh技术开发之家

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.计时类StartTimer.cs

View Code
class StartTimer
{
public uint second;
public uint minute;
public uint hour;
public Label labelTimer = new Label();
public Timer countTimer = new Timer();//定义一个计时器
//计时函数
public StartTimer()
{
labelTimer.Size
= new Size(120, 20);
this.labelTimer.Text = "00:00:00";
this.labelTimer.TextAlign = System.Drawing.ContentAlignment.TopCenter;

countTimer.Interval
= 1000;//计时定时器初始化
countTimer.Tick += new EventHandler(countTimer_Tick);
}
private string strTimer()
{
string timer = null;
string timerSecond = null;
string timerMinute = null;
string timerHour = null;
if (second < 59)
{
second
++;
}
else
{
second
= 0;
if (minute < 59)
{
minute
++;
}
else
{
minute
= 0;
if (hour < 23)
{
hour
++;
}
else
{
hour
= 0;
}
}
}
if (second < 10)
{
timerSecond
= "0" + second.ToString();
}
else
{
timerSecond
= second.ToString();
}
if (minute < 10)
{
timerMinute
= "0" + minute.ToString();
}
else
{
timerMinute
= minute.ToString();
}
if (hour < 10)
{
timerHour
= "0" + hour.ToString();
}
else
{
timerHour
= hour.ToString();
}
timer
= timerHour + ":" + timerMinute + ":" + timerSecond;
return timer;
}
//重置计时
public void resetTimer()
{
second
= 0;
minute
= 0;
hour
= 0;
labelTimer.Text
= "00:00:00";
}
//采集计时
private void countTimer_Tick(object sender, EventArgs e)
{
labelTimer.Text
= strTimer();
}
}

2.调用

StartTimer timer=new StartTimer();//新建一个实例

this.Controls.Add(startTimer.labelTimer);//将时间label添加到当前的Form中
startTimer.labelTimer.Location = new Point(120,220);//指定时间label的位置
startTimer.countTimer.Enabled = true;//开始计时

posted on 2011-04-21 15:58  pojowsh  阅读(1026)  评论(0编辑  收藏  举报