0

我目前正在学习如何在 C# 中使用秒表属性。我有一个button1启动秒表。我想将时间重置为 0 并在第二次单击后重新开始,依此类推。每次连续单击按钮后,如何重置秒表计时器?

代码

namespace stopwatch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();    
        }

        Stopwatch sw = new Stopwatch();

        private void button1_Click(object sender, EventArgs e)
        { 
            sw.Start();   
            TimeSpan x = sw.Elapsed;
            textBox1.Text = x.ToString();                   
        }                
    }
}
4

3 回答 3

1

使用Stopwatch.StartNew()

试试这段代码片段:

private void button1_Click(object sender, EventArgs e)
{
    sw = Stopwatch.StartNew();

    // ... do something ...

    sw.Stop();

    textBox1.Text = sw.Elapsed.ToString();
}
于 2012-10-24T14:46:10.170 回答
0
    private void button1_Click(object sender, EventArgs e)
    {
            sw.Stop();

            TimeSpan x = sw.Elapsed;

            textBox1.Text = x.ToString();

            sw.Restart();
     }
于 2012-10-24T14:47:42.197 回答
0
Stopwatch sw = new Stopwatch();
private void button1_Click(object sender, EventArgs e)
{
    sw.Stop();
    TimeSpan x = sw.Elapsed;
    textBox1.Text = x.ToString();

    // >= .NET 4
    sw.Restart();    

    // >=.NET 2.0
    sw.Reset();
    sw.Start();                   
}
于 2012-10-24T14:46:13.380 回答