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 ...

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.**
End Sub

Recommended Answers

All 15 Replies

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

datetime.AddSeconds(-1) ' Decrement by one second

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

if datetime.Minute <> 0 then
end if

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
        DirectCast(sender,Timer).Enabled = False
    end if

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

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

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.

"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

Try this:

        Private Sub ... Handles timer1.tick
            If datetime.Minute > 0 Then
                CountDownBox.text = datetime.ToLongTimeString()             
                DirectCast(sender,Timer).Enabled = False
            end if

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.

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.

How are you intializing datetime?

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
' 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.

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)
   End Sub

   Private Sub tmCountDown_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmCountDown.Tick
      tsStartTime = tsStartTime.Subtract(OneSecond)
      ' 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
         lblCountDown.BackColor = SystemColors.Window
      End If
      lblCountDown.Text = tsStartTime.ToString.Replace("."c, ":"c)
   End Sub
End Class

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.