c# - How can I convert the timer from counting back in seconds to count back in minutes and seconds? -
i have numericupdown1 when set value it's saving value in options text file:
private void numericupdown1_valuechanged(object sender, eventargs e) { options_db.set_radar_images_time(numericupdown1.value); }
timer1 interval set 1000ms in form1 designer.
in timer1 tick event have:
private void timer1_tick(object sender, eventargs e) { numbers_radar = convert.toint64(numericupdown1.value); }
now want assign timer tick event to: label21.text , display minutes counting down. if set numericupdown1 10 count downwards 10 minutes.
the format should like: minutes:seconds (00:00).
and each time timer 1 should phone call method: filedownloadradar(); each time when it's 1 timer should reset numericupdown1 value , start on 1 time again counting , each time in end phone call method filedownloadradar();
the numericupdown1 set minimum 5 , maximum 60
edit
now tried code don't see alter on label21 when starting timer. , minutes starting 0 should in case 29(value of numericupdown1).
and should check if minutes , seconds == 1 or == 0 ? what's more logic 1 or 0 ?
private void timer1_tick(object sender, eventargs e) { numofminutes = convert.toint32(numericupdown1.value); int seconds = numofminutes % 60; int minutes = numofminutes / 60; seconds --; string time = minutes + ":" + seconds; label21.text = time; if (seconds == 1) { minutes --; } if (minutes == 1 && seconds == 1) { numofminutes = convert.toint32(numericupdown1.value); filedownloadradar(); } }
i think improve utilize timespan object , start follows.
declare timespan variable in object (thus private field):
private timespan span;
just below code start timer, initialize span variable:
timer1.start(); // should exist somewhere timespan span = new timespan(0, numericupdown1.value, 0);
in timer event handler, write code:
private void timer1_tick(object sender, eventargs e) { span = span.subtract(new timespan(0, 0, 1)); label21.text = span.tostring(@"mm\:ss"); if (span.totalseconds < 1) { span = new timespan(0, numericupdown1.value, 0); filedownloadradar(); } }
i'm not sure want in if statement, hope help further.
c# .net winforms
No comments:
Post a Comment