Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

c# - Remove specified multiples of 60 from a number?

So I'm trying to make a Stopwatch program and I can't figure out how to format the seconds and mintues correctly because every minute or 60 minutes, it doesnt reset the seconds/minutes that are displayed back to 0. I've been searching for something to just simply format the seconds/minutes in a way that makes it looks normal.

My code so far (it's just a snippet):

updater.Tick += (sender, e) =>
{
    hours = (int)stopwatch.Elapsed.TotalHours;
    minutes = (int)stopwatch.Elapsed.TotalMinutes;
    seconds = (int)stopwatch.Elapsed.TotalSeconds;

    if (hours > 24)
    {
        MessageBox.Show("Time limit exceeded. Please reset Stopwatch to continue.", "Time Limit Exceeded", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    lblHours.Text = string.Format("{0:D2}", hours);
    lblMinutes.Text = string.Format("{0:D2}", minutes);
    lblSeconds.Text = string.Format("{0:D2}", seconds);

    Application.DoEvents();
};

If someone could help me figure this out, then I would really appreciate it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't want TotalMinutes and TotalSeconds, use Minutes and Seconds instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...