-
- Drag a label and a timer to the main interface and set the timer interval to 1000.
-
- Add a global variable to indicate the time point after 40 minutes (exam time.PrivateDatetime endtime;
- Set the endtime value in the window loading event and enable timer. Private Void Form1_load ( Object Sender, eventargs E)
{
Endtime=Datetime. Now. addminutes (40);
Timer1.enabled= True;
}
- In the timer tick event, use endtime minus the current time to get the remaining time, so as to implement the countdown. Private Void Timereffectick ( Object Sender, eventargs E)
{
Datetime show=Start. Subtract (NewTimespan (datetime. Now. ticks ));
Label1.text=Show. tolongtimestring ();
}
note this method: datetime. subtract (), which has more than one overload:
datetime subtract (timespan) and timespan subtract (datetime). When the parameter is of the datetime type, the return value is of the timespan type, when the parameter is timespan, the return value is of the datetime type.