tags:

views:

28

answers:

1

I have this problem:

I have a vb.net class (visual studio 2010) :

....
Private nowTime As String
....
Protected Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

  nowTime = My.Computer.Clock.LocalTime.ToLongTimeString

....

End Sub

How can I get the first value of "nowTime" String variable after having the Timer enabled for any interval of time?

Thanks in advance!

+1  A: 

Something like this should work.

....
Private nowTime As String
Private firstTime As String
Private isFirstTriggered As Boolean = False
....
Protected Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

  If Not isFirstTriggered Then
    firstTime = My.Computer.Clock.LocalTime.ToLongTimeString
    isFirstTriggered = True
  End If
  nowTime = My.Computer.Clock.LocalTime.ToLongTimeString

....

End Sub
rockinthesixstring
note: i would recommend storing the variable as a DateTime until you're actually ready to display it as a String. It will just help a little with performance.
rockinthesixstring
Your last note triggered me another issue with datetime:Which function shall I call to have the format of DateTime.Now() exactly the same with the format of " My.Computer.Clock.LocalTime.ToLongTimeString"Or shall I post another question?Thanks In advance.
Novemberland
I think you just use `My.Computer.Clock.LocalTime`. the `.ToLongTimeString` can be used to convert the `Date` at a later time
rockinthesixstring
I mean if I should use DateTime.Now() instead of My.Computer.Clock.LocalTime.ToLongTimeStringand having the same format. That is because I have already a DateTime variable (with the desired format of My.Computer.Clock.LocalTime.ToLongTimeString) and I want to subtract from it DateTime.Now() but they are not of the same format so I have to alter the format of DateTime.Now() to the one expected otherwise I have milliseconds appearing.
Novemberland
I would personally use DateTime.Now() simply because it's what I'm used to. I don't know exactly what your app is doing, so all I can say is "try it"
rockinthesixstring
+1 for your answers
Novemberland
Is the answer to your (original) question "right" or just a +1?
rockinthesixstring
Lets see if other answers exist and accordingly we choose
Novemberland