Search code examples
c#timercountdowncountdowntimer

TimeSpan countdown timer


I am trying to display a countdown in my application with:

private TimeSpan ts = new TimeSpan()


ts = ts.Subtract(TimeSpan.FromSeconds(1));
label4.Text = ts.ToString(@"hh\:mm\:ss");

However, it does not display a countdown but the time actually increases. If I remove the @"hh\:mm\:ss" part, then I do get a countdown in seconds but not a true countdown. All it does is that it adds a minus sign before the seconds, so it does -1, -2, -3, etc... I want it to go say from 10 to 0 so -9, -8, -7, etc...

How can I have a true countdown in the form of hh:mm:ss?


Solution

  • Initialize ts to 10: (You are currently initializing it to 0 and decrementing it)

    private TimeSpan ts = TimeSpan.FromSeconds(10)
    ...
    ts = ts.Subtract(TimeSpan.FromSeconds(1));
    label4.Text = ts.ToString(@"hh\:mm\:ss");