求C#正计时代码(分不够再加)

woaini826 2009-06-03 10:18:22
我想做一个wiform正计时,就是当程序运行后就开始计时,从00:00:00开始计时,每一秒加一。最好还可以暂停,当点击暂停的时候就停止计时,恢复的时候就继续计时。希望大家帮我想想,也可以说说做的思路。感激不尽。
...全文
87 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yagebu1983 2009-06-03
  • 打赏
  • 举报
回复
自己可以开发的控件。。。
woaini826 2009-06-03
  • 打赏
  • 举报
回复
看了下。他的复杂点,但功能强大点,你的简单易懂,功能也实现了。但是停止后,在开始不是从0开始的。而是从上次停止的时间开始的。最好对于只有1位的前面补0.不过还是感谢你,帖子我会另外加分的。
wjn161 2009-06-03
  • 打赏
  • 举报
回复
傻瓜式的实现方法~
button1为开始,button2为暂停, button3为停止~

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public int s=0;
public int m = 0;
public int h = 0;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
s++;
this.label1.Text = h.ToString()+":"+m.ToString()+":"+s.ToString();
if (s==60)
{
m += 1;
s = 0;
}
if (m==60)
{
h += 1;
m = 0;
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;

}

private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}

private void button3_Click(object sender, EventArgs e)
{
timer1.Stop();
timer1.Enabled = false;
label1.Text = "0:0:0";
}

}
}

woaini826 2009-06-03
  • 打赏
  • 举报
回复
太谢谢你了。每次都得到你的帮助。真是感激不尽。这分数全部给你了。
wuyq11 2009-06-03
  • 打赏
  • 举报
回复
基于 Windows 的标准计时器(System.Windows.Forms.Timer)
基于服务器的计时器(System.Timers.Timer)
线程计时器(System.Threading.Timer)

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;
if(f<10)
{
fff = "0" + f.ToString();
}
else
{
fff = f.ToString();
}
if(s<10)
{
ss = "0" + s.ToString();
}
else
{
ss = s.ToString();
}
if(m<10)
{
mm = "0" + m.ToString();
}
else
{
mm = m.ToString();
}
if(h<10)
{
hh = "0" + h.ToString();
}
else
{
hh = h.ToString();
}
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);
}
}
参考

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧