Search code examples
c#winformstimerstopwatch

How to increment in time?


I am creating a stopwatch type application. I've used a label to display time. When application starts, the label is initialized to '00:00:00' and I want to increment its time by every 1 second.

I am trying to accomplish this job by using timer.


Solution

  • If I am getting you right, it may surely work. Set initial label Text as "00:00:00". Set timer interval as 1000.

    private void btnStartWatch_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }
    
    private void btnPauseWatch_Click(object sender, EventArgs e)
    {
        timer1.Enabled = false;
    }
    
    int i = 1;
    DateTime dt = new DateTime();
    private void timer1_Tick(object sender, EventArgs e)
    {
        label1.Text = dt.AddSeconds(i).ToString("HH:mm:ss");
        i++;
    }
    

    Hope it helps.