Timer to count down in VB.Net 2010? -


i'm trying use timer count down specified time choose time being separated minutes , seconds using format mm:ss , stop when time reaches 00:00.

so far i've used previous answer found on here , modified best of knowledge counting down although i've hit snag in when timer starts counting down, it's delayed , out of sync when counting down minutes.

for example, counting down 120 seconds;

02:00 > 02:59 > 02:58 > 02:57 > 02:56 > 02:55

and when continuing count down past 90 seconds under same test;

02:30 > 01:29 > 01:28 > 01:27 > 01:26 > 01:25

when countdown reaches 00 or 30 seconds, incorrectly displays minutes left , can't understand or figure out how fix it.

here code counting timer;

private sub tmrcountdown_tick(byval sender system.object, _                               byval e system.eventargs) _                           handles tmrcountdown.tick      settime = settime - 1     lbltime.text = formattime(settime)      if settime = 0         tmrcountdown.enabled = false     end if  end sub 

here code function formatting time;

public function formattime(byval time integer) string     dim min integer     dim sec integer      'minutes     min = ((time - sec) / 60) mod 60      'seconds     sec = time mod 60      return format(min, "00") & ":" & format(sec, "00")  end function 

and here code form load;

private sub frmsingleplayer_load(byval sender system.object, _                                  byval e system.eventargs) _                              handles mybase.load      'setting time.     settime = 120     lbltime.text = formattime(settime)     tmrcountdown.enabled = true  end sub 

i've set;

    dim settime integer 

at top of public class able input specified time countdown timer. incredibly silly , can't figure out is.

any appreciated , please bare in mind, beginner @ programming , confused large walls of code. (i can barely understand function is.)

thank helping!

play this:

public class frmsingleplayer      private targetdt datetime     private countdownfrom timespan = timespan.fromminutes(3)      private sub frmsingleplayer_load(sender system.object, e system.eventargs) handles mybase.load         tmrcountdown.interval = 500         targetdt = datetime.now.add(countdownfrom)         tmrcountdown.start()     end sub      private sub tmrcountdown_tick(sender object, e system.eventargs) handles tmrcountdown.tick         dim ts timespan = targetdt.subtract(datetime.now)         if ts.totalmilliseconds > 0             lbltime.text = ts.tostring("mm\:ss")         else             lbltime.text = "00:00"             tmrcountdown.stop()             messagebox.show("done")         end if     end sub  end class 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -