用C#做个简单的计时器(秒表功能)
来源:互联网 发布:黑龙江省网络选号 编辑:程序博客网 时间:2024/05/16 04:56
先在窗口中加入一个计时器控件:(timer1)用于触发计时;一个Label控件:(bable1)用于显示时间;两个按钮:(btnStar)用于开始/停止计时,(btnClear)用于计时器清零。
声明一个整型变量:t,用于获取毫秒,然后在窗口代码中加入以下代码:
private int t = 0;
void MainFormLoad(object sender, System.EventArgs e)
{
this.timer1.Enabled = false;
this.timer1.Interval = 1;
}
//计时函数
public string GetAllTime(int time)
{
string hh, mm, ss, fff;
int f = time%100; // 毫秒
int s = time/100; // 转化为秒
int m = s/60; // 分
int h = m/60; // 时
s = s%60; // 秒
//毫秒格式00
if(f<10)
{
fff = "0" + f.ToString();
}
else
{
fff = f.ToString();
}
//秒格式00
if(s<10)
{
ss = "0" + s.ToString();
}
else
{
ss = s.ToString();
}
//分格式00
if(m<10)
{
mm = "0" + m.ToString();
}
else
{
mm = m.ToString();
}
//时格式00
if(h<10)
{
hh = "0" + h.ToString();
}
else
{
hh = h.ToString();
}
//返回 hh:mm:ss.ff
return hh + ":" + mm + ":" + ss + "." + fff;
}
//开始计时按钮单击事件
void BtnOKClick(object sender, System.EventArgs e)
{
if(timer1.Enabled == false)
{
this.btnOK.Text = "停止计时";
this.timer1.Enabled = true;
}
else
{
this.btnOK.Text = "开始计时";
this.timer1.Enabled = false;
}
}
//时钟控件事件
void Timer1Tick(object sender, System.EventArgs e)
{
t = t + 1;//得到总的毫秒数
this.label1.Text = GetAllTime(t);
}
//计时器清零
void BtnClearClick(object sender, System.EventArgs e)
{
t = 0;
//如何正在计时,则先停止再清零,否则直接清零
if(this.timer1.Enabled == true)
{
this.BtnOKClick(sender,e);
label1.Text = GetAllTime(t);
}
else
{
label1.Text = GetAllTime(t);
}
}
声明:非原创,载自网络
- 用C#做个简单的计时器(秒表功能)
- 用C#做个简单的计时器(秒表功能)
- 简单的Java秒表计时器(线程)
- 简单计时器的实现方法(类似秒表)
- JS简单的按钮秒表计时器1
- Stopwatch计时器、秒表 C#
- .NET C# 秒表 计时器
- Stopwatch计时器、秒表 C#
- 秒表(计时器)
- Java计时器(秒表)
- 使用实现Runnable的多线程,实现秒表计时器功能
- 一个简单的java秒表java计时器,用java语言写的,android开发
- JS简单的鼠标按键秒表计时器2
- 一个自定义的秒表计时器
- 【Java】Java计时器(秒表)
- C#基础之秒表的简单实现
- 用cocos2dx实现一个秒表级的计时器。
- Android我自己的简易(秒表)计时器Chronometer
- reverse 函数,countVowels 函数,EL函数的使用
- IT知名企业常见面试题实例与详解
- SQL常用代码 函数 精妙算法
- 半路出家
- 本地计算机上的OracleOraHome92Agent服务启动后又停止了
- 用C#做个简单的计时器(秒表功能)
- 回忆我在日本和日本人的斗争(三)
- 安装Apache
- “法”定节假日
- gcc gdb的使用
- VS 2005实用键
- 千万不要攻击中国网站!传奇美国黑客凯文·米特尼克的警告
- 文本编辑器使用技巧
- C++编译时函数名修饰约定规则