Countdown Timer

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 3
Reputation: dudeysi is an unknown quantity at this point 
Solved Threads: 0
dudeysi dudeysi is offline Offline
Newbie Poster

Countdown Timer

 
0
  #1
Oct 9th, 2006
I am currently making a timer for a school project. Im a little new to VB.net and having a few problems getting the timer to work.

When the countdown timer gets to 0 the "-" sign appears in the wrong place. As in i get 0:-1

I would also like the text in the timer to turn red when it enters negative numbers. (Maybe Flash, but i guess that is very complicated).

Anyway here is my current code:


Public Class Form1
Inherits System.Windows.Forms.Form
Dim TargetTime As System.DateTime
Dim willy As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
willy = TextBox1.Text
If Timer1.Enabled Then
Timer1.Stop()
Else
TargetTime = Now.AddMinutes(willy)
Timer1.Start()
End If
Catch ex As Exception
MsgBox("No Time Set")
End Try
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Diff As TimeSpan = TargetTime.Subtract(Now)
Label1.Text = Diff.Minutes & ":" & Diff.Seconds
End Sub
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 6
Reputation: Danny.VBT is an unknown quantity at this point 
Solved Threads: 0
Danny.VBT's Avatar
Danny.VBT Danny.VBT is offline Offline
Newbie Poster

Re: Countdown Timer

 
0
  #2
Oct 10th, 2006
I'm assuming you are trying to get an output of -0:1...etc..etc...?

Try wrapping the difference in Math.abs() and then concatenating a '-' at the if the output is negative.
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Countdown Timer

 
0
  #3
Oct 10th, 2006
Try this:
  1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2. Dim Diff As TimeSpan = TargetTime.Subtract(Now)
  3. If Diff.Seconds < 0 Then
  4. Label1.Text = "-" & Format(Diff.Minutes, "#0") & ":" & Format(System.Math.Abs(Diff.Seconds), "00")
  5. Else
  6. Label1.Text = Format(Diff.Minutes, "#0") & ":" & Format(System.Math.Abs(Diff.Seconds), "00")
  7. End If
  8. End Sub
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: dudeysi is an unknown quantity at this point 
Solved Threads: 0
dudeysi dudeysi is offline Offline
Newbie Poster

Re: Countdown Timer

 
0
  #4
Oct 10th, 2006
Thanks alot wayne, that worked a treat.

Any idea on how to get the label text to turn red in negatives?

tried loads of different things but none seem to work
Last edited by dudeysi; Oct 10th, 2006 at 2:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: dudeysi is an unknown quantity at this point 
Solved Threads: 0
dudeysi dudeysi is offline Offline
Newbie Poster

Re: Countdown Timer

 
0
  #5
Oct 10th, 2006
ok now im using the code that wayne posted, but the minus's arnt quite working properly. with waynes code it works for -0:01 to -0:59 but goes to --01:01. Any suggestions?
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Countdown Timer

 
0
  #6
Oct 13th, 2006
Try this:
  1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2. Dim Diff As TimeSpan = TargetTime.Subtract(Now)
  3. If Diff.Seconds < 0 And Diff.Minutes >= 0 Then
  4. Label1.Text = "-" & Format(Diff.Minutes, "#0") & ":" & Format(System.Math.Abs(Diff.Seconds), "00")
  5. Else
  6. Label1.Text = Format(Diff.Minutes, "#0") & ":" & Format(System.Math.Abs(Diff.Seconds), "00")
  7. End If
  8. End Sub
Sorry it took so long in replying.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC