|
-
Aug 2nd, 2010, 02:11 PM
#1
Thread Starter
Member
-
Aug 2nd, 2010, 02:45 PM
#2
Frenzied Member
Re: Countdown timer help
No one will do your work for you. However we will try and help you as much as possible. Also, if there isn't that much code, then please just post the relevant code using the code tags.
-
Aug 2nd, 2010, 03:04 PM
#3
Thread Starter
Member
Re: Countdown timer help
Ok then. So I have 3 labels. minutes hours and seconds. 2 buttons for stop and start. Also 1 timer.
here is the code for the timer
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label3.Text = ("0") Then
If Label3.Text = ("0") And Label2.Text = ("0") And Label1.Text = ("0") Then
Timer1.Stop()
MsgBox("DONE!")
End If
Label3.Text = ("59")
Label2.Text -= 1
End If
If Label2.Text = ("0") Then
Label2.Text = ("59")
Label1.Text -= 1
End If
End Sub
Any ideas to help get is to count down from 1 hour. It works for 30 minutes and 10 seconds.
P.S. The clock is set to 1 hour and 0 minutes and 0 seconds.
Sorry for my post before
-
Aug 2nd, 2010, 03:25 PM
#4
Re: Countdown timer help
It's always best to post code in here, as people are really hesitant about downloading attachments.
A couple preliminary notes that are unrelated to the ultimate solution:
1) Use AndAlso rather than And. If you use And, then all parts of the expression are evaluated. If you use AndAlso, then the expression will be evaluated from left to right, but only as far as is necessary. Therefore, if the first part of the expression is False, there is no reason to check the other parts, as the whole expression MUST be False. There is also an OrElse.
2) Better label names would certainly be easier to read. I am assuming that Label1 is the hours, Label2 is the minutes, and Label3 is the seconds, but it would be easier if they were named like that.
On to more substantive issues.
1) Turn Option Strict ON for all projects. It leads to better and more efficient code, and the minor amount of extra typing you will then have to do will be more than offset by the good habits learned. In this case, you are subtracting 1 from a label, which can't normally happen, because a label is a string. What is happening is that the string is being quietly turned into an integer, 1 is being subtracted, then the integer is being quietly turned back into a string. Doing that explicitly will improve performance (though you won't see it because the code is so fast anyways that even a 50% improvement, which you might well have, would be unnoticeable.)
2) When Label3 is zero, you check to see that all three are zero. You don't actually need to check Label3 again in this code, because you already know it is zero. However, you then set it to 59, and decrement Label2. You do this even if all three are zero. I would suggest that those two steps be put into an Else statement so that if all three are zero, they are left zero, and only if they are not zero will the decrement happen.
3) The key issue is that when Label3 reaches zero, you check all three, but if the others are not zero, you ALWAYS decrement Label2. In fact, you should be checking to see if Label2 is zero, and if it is, then decrement Label1 and set Label2 to 59. Thus you need just one more step. You almost have it in that second If statement, but it isn't quite right. Label2 will reach zero and be set to 59 a minute before it should. You could get rid of that second If statement by just nesting a check of Label2 into the first one.
My usual boring signature: Nothing
 
-
Aug 2nd, 2010, 03:46 PM
#5
Re: Countdown timer help
Use a timespan and initialize it to your start value (say, 1 hour).
Private ts as TimeSpan = New TimeSpan(1, 0, 0)
In your timer.tick, you do:
1. substract the timer.interval from the timespan.
2. Test the timespan's new value
if the ticks <=0 then
set timespan = 0
stop timer
end if
3. Display the result to user
labelHr.text = timespan.Hours.tostring
labelMinute.text = timespan.Minutes.Tostring
...
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Aug 3rd, 2010, 03:00 PM
#6
Thread Starter
Member
Re: [RESOLVED] Countdown timer help
Thanks for the help you guys!!! I got the code working (I think )
Anyways here is the sourcecode if you want to use it.
Also if you can test out the timer with different intervals of time, that would mean alot.
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label3.Text = ("0") Then
If Label2.Text = ("0") Then
If Label1.Text = ("0") Then
Timer1.Stop()
MsgBox("DONE!")
Else
Label2.Text = ("59")
Label3.Text = ("59")
Label1.Text -= 1
End If
Else
Label3.Text = ("59")
Label2.Text -= 1
End If
Else
Label3.Text -= 1
End If
End Sub
-
Aug 3rd, 2010, 03:32 PM
#7
Re: [RESOLVED] Countdown timer help
You should take shaggy's suggestions...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 250
Timer1.Start()
End Sub
Dim isCountingDown As Boolean = False
Dim stopAt As DateTime
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If isCountingDown Then Exit Sub 'one countdown at a time
'stopAt = DateTime.Now.AddSeconds(10) '10 second countdown
stopAt = DateTime.Now.AddHours(1) 'set stop point to be one hour in the future
isCountingDown = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
If isCountingDown Then 'are we counting down
'yes
Dim dt As DateTime = DateTime.Now 'get the current dt
If dt >= stopAt Then 'count down over
'yes
isCountingDown = False
Label1.Text = "Blast Off"
Else
'no, show time remaining
Dim tr As TimeSpan = stopAt - dt
Label1.Text = String.Format("{0}:{1}:{2}", _
tr.Hours.ToString.PadLeft(2, "0"c), _
tr.Minutes.ToString.PadLeft(2, "0"c), _
tr.Seconds.ToString.PadLeft(2, "0"c))
End If
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|