简体   繁体   中英

How to make a stopwatch windows form application

I am currently trying to make a chronometer in c# using the timespan class. So far I have been able to appropriately start, pause, and stop the chronometer, but I have been asked to make a lap button that registers the time in the chronometer upon click, and make another button that opens another form to list said lap times. It is this part that i have trouble with.

Basically I need help with registering the time and retaining those values to later show them in a list. I appreciate your time and willingness to help. This is some of the code i tried to make for registering the time along with making a different class called LapList, it didnt go very well.

private void button2_Click(object sender, EventArgs e)
{            
    TimeSpan Et = Crono.Elapsed;
    TimeSpan LapTime = Et - LastBreakTime;
    LastBreakTime = Et;
    ++Lapcount;
    LapList.getTimeSpan().Add(LapTime);
}

Thanks again for your time.

It seems you should be able to store the DateTime.Now of each click of the Lap button, then use DateTime2 - DateTime1 to give you a TimeSpan object that can be displayed?

So each click of Lap button effectively performs a List.Add(DateTime.Now) and your lap display iterates over the list, performing List[I] - List[I-1]

.NET already has a Stopwatch for measuring time and returning the elapsed time as a TimeSpan , Milliseconds or Ticks .

You can start a new Stopwatch with Stopwatch.StartNew and store the instance in a field until you need to check the elapsed time. You can also pass the instance from method to method, or store it in an array or dictionary so you can time multiple executions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM