I am currently learning how to work with the stopwatch attribute in C#. I have a button1 that starts the stopwatch. I would like to reset the time to 0 and start over again after the second click and so forth on. How would I be able to reset stopwatch timer after each consecutive button click?
Code
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();
}
}
}
Use Stopwatch.StartNew()
Try this piece of code snippet instead: