1,105,208 Community Members

How to use timer in vb.net

Member Avatar
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
deceptikon
Eternally Awesome
4,666 posts since Jan 2012
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 679 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

You can use negative values in the Add*() methods to represent subtraction from the current value:

datetime.AddSeconds(-1) ' Decrement by one second
Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
0
 

instead of a while loop try changing it to a conditional statement,

if datetime.Minute <> 0 then
    datetime.AddSeconds(-1)
end if
Member Avatar
Reverend Jim
Noli mentula
5,394 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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
Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
0
 

unless the interval is set to 1000. Then it would fire every 1 second, and a simple conditional would do.

Member Avatar
Reverend Jim
Noli mentula
5,394 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

1000 milliseconds is one second so the statement stands.

Member Avatar
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Reverend Jim,
What is the function of DirectCast(sender, Timer)

Member Avatar
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
Reverend Jim
Noli mentula
5,394 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
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
Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
0
 

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
Member Avatar
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
0
 

How are you intializing datetime?

Member Avatar
Mr.M
Junior Poster
197 posts since Dec 2012
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 2 [?]
 
0
 

The simple way is to just count the total datetime then add it on a CountDownBox on properties then you will need to trace the shutdown events so that even if you close the program but it will keep a record of the last count before it shutdown so when you re-run it, it will then read the time from the disc. but let me first show you how I done this all. let say you want to count the 24Hours so if your timer will fire in minutes (1000) interval then here is how you count it. We know that you will be using the minutes so you start from 1000 x 60 x 24 = 1440000 so you will put the 1440000 to the properties of the CountDownBox then on your timer interval should be 1000 not 100 unless you counted from 100 not 1000 and your timer code will be like this

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
CountDownBox.Text = CountDownBox.Text - 1
If CountDownBox.text = 0 Then
Timer1.stop()
Else
' Do Nothing ather then Counting down
CountDownBox.Text = CountDownBox.Text - 1
End If
End Sub

then you have to track the shutdown events so that when you have close and rerun it wont start over and over again when ever you re-run the program. Hope I answered your Question.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

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
Member Avatar
monching
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Question Answered as of 1 Year Ago by tinstaafl, Reverend Jim, deceptikon and 2 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article