How to use timer in vb.net
Hello everyone,
I have my problem in using timer in vb. I have aleady the value, all i want is my time will count down.
my sample code is this:
Dim datetime As DateTime
Private Sub ...
datetime.AddMinutes(MinuteBox.Text)
datetime.AddSeconds(SecondBox.Text)
datetime.AddTicks(1)
End Sub
Dim remaining As Integer 'unused variable
Private Sub ... Handles timer1.tick
Do Until datetime.Minute = 0
datetime.Second -= 1 '**i am not sure in this line, this is not a valid statement.**
Loop
End Sub
Related Article: How to use select in vb.net 2010
is a solved VB.NET discussion thread by Iamkenny that has 4 replies, was last updated 1 year ago and has been tagged with the keywords: database, vb.net, select, command.
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 13
Solved Threads: 1
Skill Endorsements: 0
You can use negative values in the Add*() methods to represent subtraction from the current value:
datetime.AddSeconds(-1) ' Decrement by one second
deceptikon
Challenge Accepted
3,457 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57
instead of a while loop try changing it to a conditional statement,
if datetime.Minute <> 0 then
datetime.AddSeconds(-1)
end if
tinstaafl
Nearly a Posting Virtuoso
1,336 posts since Jun 2010
Reputation Points: 360
Solved Threads: 235
Skill Endorsements: 14
The Timer.Tick event fires once every Interval milliseconds so you don't need a loop inside the handler. If you are counting down to zero then just stop the timer at that point.
Private Sub ... Handles timer1.tick
If datetime.Minute > 0 Then
datetime.AddSeconds(-1)
Else
DirectCast(sender,Timer).Enabled = False
end if
Reverend Jim
Carpe per diem
3,623 posts since Aug 2010
Reputation Points: 563
Solved Threads: 452
Skill Endorsements: 32
unless the interval is set to 1000. Then it would fire every 1 second, and a simple conditional would do.
tinstaafl
Nearly a Posting Virtuoso
1,336 posts since Jun 2010
Reputation Points: 360
Solved Threads: 235
Skill Endorsements: 14
1000 milliseconds is one second so the statement stands.
Reverend Jim
Carpe per diem
3,623 posts since Aug 2010
Reputation Points: 563
Solved Threads: 452
Skill Endorsements: 32
Reverend Jim,
What is the function of DirectCast(sender, Timer)
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 13
Solved Threads: 1
Skill Endorsements: 0
Jim,
This doesn't work. There's no error, my program is running, but there no changes in my output.
CountDownBox.text = datetime.ToLongTime
that's how I show the datetime value.
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 13
Solved Threads: 1
Skill Endorsements: 0
"sender" is a generic object. I cast it to a Timer object so I can access the Enabled property. You could also do
Dim t As Timer = sender
t.Enabled = False
Reverend Jim
Carpe per diem
3,623 posts since Aug 2010
Reputation Points: 563
Solved Threads: 452
Skill Endorsements: 32
Try this:
Private Sub ... Handles timer1.tick
If datetime.Minute > 0 Then
datetime.AddSeconds(-1)
CountDownBox.text = datetime.ToLongTimeString()
Else
DirectCast(sender,Timer).Enabled = False
end if
tinstaafl
Nearly a Posting Virtuoso
1,336 posts since Jun 2010
Reputation Points: 360
Solved Threads: 235
Skill Endorsements: 14
In that code, the program will not enter in the first statement, it will directly go to Else statement and proceed to DirectCast were the datetime.addseconds(-1) will not be performed.
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 13
Solved Threads: 1
Skill Endorsements: 0
I think the problem here is this Dim datetime As DateTime because, the output format was like this 12:00 AM. I don't know how to change the format of this, the default format is the time of day.
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 13
Solved Threads: 1
Skill Endorsements: 0
How are you intializing datetime?
tinstaafl
Nearly a Posting Virtuoso
1,336 posts since Jun 2010
Reputation Points: 360
Solved Threads: 235
Skill Endorsements: 14
Perhaps using a TimeSpan instead of a DateTime will give you the effect you are looking for?
Public Class Form1
Private WithEvents tmCountDown As New System.Windows.Forms.Timer With {.Interval = 1000}
Private tsStartTime As TimeSpan
Private lblCountDown As New Label With {.Location = New Point(10, 10), _
.AutoSize = True, _
.Font = New Font("Deja Vu Sans", 14, FontStyle.Regular, GraphicsUnit.Point), _
.ForeColor = SystemColors.WindowText, _
.TextAlign = ContentAlignment.MiddleCenter, _
.Parent = Me}
Private OneSecond As New TimeSpan(0, 0, 1)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set these durations to whatever is needed
Dim Days As Int32 = 0
Dim Hrs As Int32 = 0
Dim Mins As Int32 = 0
Dim Secs As Int32 = 1
Dim MilliSecs As Int32 = 0
tsStartTime = New TimeSpan(Days, Hrs, Mins, Secs, MilliSecs)
DisplayTime()
tmCountDown.Start()
End Sub
Private Sub tmCountDown_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmCountDown.Tick
tsStartTime = tsStartTime.Subtract(OneSecond)
DisplayTime()
' Stop CountDown ? If so, uncomment next line
'If tsStartTime.Ticks = 0 Then tmCountDown.Stop()
End Sub
Private Sub DisplayTime()
If tsStartTime.Ticks < 0 Then
lblCountDown.BackColor = Color.Yellow
Else
lblCountDown.BackColor = SystemColors.Window
End If
lblCountDown.Text = tsStartTime.ToString.Replace("."c, ":"c)
End Sub
End Class
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13
thanks TnTinMN, that is what i mean. I've been trying to use TimeSpan few weeks ago, but i don't know how to add a value and reduce it.
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 13
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 4 Months Ago by
tinstaafl,
Reverend Jim,
deceptikon
and 2 others