在WinForm上运行数字时钟

嗨,我必须设计一个具有简单文本框的Windows窗体。 文本框包含类似文本的计时器(00:00格式)。

我想每秒刷新页面,并使文本框的内容相应更改。 (就像数字时钟一样,运行一小时!)。

我想我需要使用System.Windows.Forms.Timer类,我已经将一个Timer项从ToolBox中删除到我的Form。

下一步…我需要使用Thread.Sleep(1000)函数..任何想法?

这是我一直在尝试的代码片段。 我知道程序出错了,而thread.sleep()部分甚至使我的代码运行更糟糕。 我在ToolBox中尝试了Timer的东西,但无法通过。(当我运行代码时,它成功编译,然后应用程序由于脏For-Loops冻结了一个小时)帮助!!

  public partial class Form1 : Form { Button b = new Button(); TextBox tb = new TextBox(); //System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer(); public Form1() { b.Click += new EventHandler(b_click); b.Text = "START"; tb.Text = "00 : 00"; //timer1.Enabled = true; //timer1.Interval = 1000; tb.Location = new Point(100, 100); this.Controls.Add(tb); this.Controls.Add(b); InitializeComponent(); } private void refreshTimer_Tick() { for (int i = 0; i < 60; i++) { for (int j = 0; j < 60; j++) { Thread.Sleep(500); string TempTime = string.Format("{0:00} : {1:00}",i,j); tb.Text = TempTime; Thread.Sleep(500); } } } public void b_click(object sender, EventArgs e) { refreshTimer_Tick(); } } 

设置计时器和刷新周期。 (1秒)

 timer1.Enabled = true; timer1.Interval = 1000; 

然后你需要实现你希望计时器每1秒做一次:

 private void timer1_Tick(object sender, EventArgs e) { DigiClockTextBox.Text = DateTime.Now.TimeOfDay.ToString(); }