Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

timer has me feeling stupid (code included)

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dtCurrentDate, tmCurrentTime, nDateTime
dtCurrentDate = Format(Now, "dd/mm/yyyy")
tmCurrentTime = Format(Now, "hh:mm:ss am/pm")
nDateTime = Format(Now)
Label1.Text = "Current Date/Time is " & nDateTime
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Refresh()
End Sub

why will the time not update? Thanks
[615 byte] By [vchatlive] at [2007-11-11 10:19:56]
# 1 Re: timer has me feeling stupid (code included)
The time doesn't update because your not changing the label in the tick event. The 'Refresh' method doesn't do what I believe you think it does. It just forces the control to redraw itself, but you haven't changed the text property so nothing will change. The code to update the label is in your forms load load event. This only gets called once when the form loads. In order to update the label you need to set the labels text property to the new value whenever the tick event occurs. Also if you haven't set it in the designer you will probably want to set the timers interval to 1000 ms (1 second), unless you want it to be longer but there shoudn't be any reason to set it shorter.

Also what's with these to variables?
dtCurrentDate = Format(Now, "dd/mm/yyyy")
tmCurrentTime = Format(Now, "hh:mm:ss am/pm")

You don't appear to be using them.
TwoFaced at 2007-11-11 20:48:08 >
# 2 Re: timer has me feeling stupid (code included)
what would i do in order to get the label1 to update correctly at this point then based on the code i provided? i tried changing the timer1_tick (interval 10) to

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = "Current Date/Time is " & nDateTime
Label1.Refresh()
End Sub

still doesnt appear to be working. Thanks!
vchatlive at 2007-11-11 20:49:13 >
# 3 Re: timer has me feeling stupid (code included)
what would i do in order to get the label1 to update correctly at this point then based on the code i provided? i tried changing the timer1_tick (interval 10) to

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = "Current Date/Time is " & nDateTime
Label1.Refresh()
End Sub

still doesnt appear to be working. Thanks!An interval of 10 is much to fast. The interval is in milliseconds thus 1000 = 1 second. Your only displaying a time accurate to a second so there isn't any need to update the label that quickly. Also calling the 'Refresh' method isn't needed, the control will update itself when you set it's text property. In order to update the control just use:

label1.text = Format(Now, "dd/mm/yyyy hh:mm:ss tt")
TwoFaced at 2007-11-11 20:50:13 >