在WinForm上运行数字时钟

Let*_*net 6 c# timer winforms

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

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

我想我需要使用System.Windows.Forms.Timer该类,我已经Timer从ToolBox中删除了一个项目到我的表单.

下一步......我需要使用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();

    }
}
Run Code Online (Sandbox Code Playgroud)

edi*_*ode 18

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

timer1.Enabled = true;
timer1.Interval = 1000;
Run Code Online (Sandbox Code Playgroud)

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

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