I'm just trying to make a basic mm:ss timer for a little sports scoreboard.
I currently have
int i = 0;
private void matchTimer_Tick(object sender, EventArgs e)
{
i++;
timeDisplay.Text = i.ToString("00:00");
}
But this fails because it doesn't account for minutes, so once time gets to 60, it just carries on
00:60
00:61
00:62
...
But I want it to be
01:00
01:01
01:02
...
and then stop at 80 minutes
You have more problems than you think. You're currently relying on the timer ticking exactly once per second. Don't do that - instead, use a System.Diagnostics.Stopwatch
to measure the elapsed time, and then update the display by formatting the value of Stopwatch.Elapsed
:
timeDisplay.Text = stopwatch.Elapsed.ToString("mm':'ss");
(See the Custom TimeSpan format strings documentation for more details.)
See more on this question at Stackoverflow