c#计时器问题 想在窗体里做一个计时器。。显示从打开这个窗体到现在的秒数,秒数精确到一秒。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Timer难道不行吗?设置Interval为1000 窗体添加Timer 控件设置Interval为1000 int num =0; // 放事件外面// Tick 事件 private void timer1_Tick(object sender, EventArgs e) { //toolStripStatusLabel3 是你要显示的标签名 //this.toolStripStatusLabel3.Text = "当前时间:"+DateTime.Now.ToString(); this.toolStripStatusLabel3.Text = "已打开时间:" + num.ToString() + "秒"; num++; } 在窗体的Load事件里,timer.start()开启时钟,即打开窗口开始计时,timer的interval属性设置1000,即1秒设置个变量i初始值为0,时钟的tick事件里,i++,即每过一秒i加一,然后弄个控件显示i值就行了大致思路就是这样 在窗体Form的load末尾timer控件的属性Enabled=true;其他同上 就是不知思路有没问题,可能有误差…… Timer 有多个 Form下面的那个Timer不准 你用Thread里的那个 系统有timerForms.TimerThreading.TimerTimers.Timer如果你觉得不爽,可以自己实现个timer新建控制台程序,用如下代码替换Program.cs中的所有内容,运行即可。using System;using System.Windows.Forms;using System.Threading;namespace CSharpConsole03{ class Program { public class Form1 : Form { protected override void OnLoad(EventArgs e) { base.OnLoad(e); StartTimer((EventHandler)delegate { DoSomething(); }, 1000); } bool running = false; public void StartTimer(Delegate d, int interval) { running = true; new Thread((ThreadStart)delegate { while (running && !this.IsDisposed) { Thread.Sleep(interval); try { this.Invoke(d); } catch (InvalidOperationException) { //skip this exception } } }).Start(); } public void StopTimer() { running = false; } public void DoSomething() { this.Text = DateTime.Now.ToString("YYYY-MM-dd HH:mm:ss"); } } static void Main(string[] args) { Application.Run(new Form1()); Console.ReadKey(); } }} WPF的Tabcontrol的selectionChange事件 急求!!大家来看看,这段程序有什么问题,为什么点击button没有反应呢?? 怎样在改别人的程序中提高自己? c#如何开发单点登陆系统 jpg,Gif 是否能 转成 rtf 怎么实现c# windows 应用程序开机自动运行? 200分求oledbConnection取表信息的问题 ★★★C#中的字符串如何比较大小呀??★★★ ASP.NET的发展前景怎么样 c# keypress(急,在线) 关于Ajax返回代码为404问题? vs2010里面建的.framework2.0的网站项目里面没有ajax选项卡。请问怎样解决。
窗体添加Timer 控件
设置Interval为1000
int num =0; // 放事件外面
// Tick 事件
private void timer1_Tick(object sender, EventArgs e)
{
//toolStripStatusLabel3 是你要显示的标签名
//this.toolStripStatusLabel3.Text = "当前时间:"+DateTime.Now.ToString();
this.toolStripStatusLabel3.Text = "已打开时间:" + num.ToString() + "秒";
num++;
}
你用Thread里的那个
Forms.Timer
Threading.Timer
Timers.Timer
如果你觉得不爽,可以自己实现个timer
新建控制台程序,用如下代码替换Program.cs中的所有内容,运行即可。using System;
using System.Windows.Forms;
using System.Threading;namespace CSharpConsole03
{
class Program
{ public class Form1 : Form
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
StartTimer((EventHandler)delegate { DoSomething(); }, 1000);
}
bool running = false;
public void StartTimer(Delegate d, int interval)
{
running = true;
new Thread((ThreadStart)delegate
{
while (running && !this.IsDisposed)
{
Thread.Sleep(interval);
try
{
this.Invoke(d);
}
catch (InvalidOperationException)
{
//skip this exception
}
}
}).Start();
} public void StopTimer()
{
running = false;
} public void DoSomething()
{
this.Text = DateTime.Now.ToString("YYYY-MM-dd HH:mm:ss");
}
} static void Main(string[] args)
{
Application.Run(new Form1());
Console.ReadKey();
}
}
}