Click here to Skip to main content
16,020,811 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am newbie in Timer
So why i want to make Sport Timer(just like real time sport timer)
i wrote the following code but it isn't same as real timer :(( . Help Me Please

Here it is ...

Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tmrDyncsub As System.Windows.Forms.Timer
        tmrDyncsub = CType(sender, System.Windows.Forms.Timer)
        Dim AlltheTime As String
        ' _millisecond += 1
        ms += 1
        If ms = 100 Then
            ms = 0
            sec += 1
        End If
        If sec = 60 Then
            sec = 0
            min += 1
        End If
        AlltheTime = min.ToString() & " : " & sec.ToString() & " : " & ms.ToString()
        tmrElapse(TimerCountClick) = AlltheTime
        'If _millisecond = 11 Then
        '    MsgBox("Hola People")
        'End If
        Me.Text = AlltheTime
        'If sec = 5 Then
        '    tmrDyncsub.Stop()
        '    MsgBox(AlltheTime)
        '    Me.Text = "123"
        'End If
    End Sub

if possible Hlep Me.
Please
Posted
Updated 28-Mar-10 17:06pm
v2

If you're looking for elapsed time all you need to do is save the current datetime when the stopwatch is started and compare it to the current datetime when is is stopped, or when the timer event is triggered. No need to maintain minutes, second or milliseconds on your own.
 
Share this answer
 
What Mark meant was something more like:

'Store the current time when you start your timer
Dim startTime As DateTime
startTime = DateTime.Now;

'Inside Tmr_Tick you can just check how much time has passed
Dim elapsed As TimeSpan
elapsed = startTime - DateTime.Now;


You can then get the number of hours, minutes, seconds and milliseconds fro m that TimeSpan.

It is also worth mentioning that the standard Timer doesn't have a very high resolution (as you may have noticed). You currently trying to set your Interval to 1 but the best that you can hope for is closer to 20ms.

One last thing, instead of adding an answer to your own question with additional information you should edit your original question, this makes it easier for other people to help you.
 
Share this answer
 
hi Mark, I think ms need to increase. u said save datetime first, how do i. I try to Add millisecond only. see the following code but it isn't work. Pls, how do i do ? Help me... :((

VB
Dim tmr As System.Windows.Forms.Timer
   Dim ts1 As New TimeSpan(0, 0, 0, 0)

VB
Dim ms, sec, min As Integer


VB
Private Sub Ticker_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Text = ts1.Seconds & ":" & ts1.Milliseconds
        ms += 15.4
        ts1 = New TimeSpan(0, 0, min, sec, ms)
    End Sub


VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'MsgBox("Ok, Let's start")
        tmr.Start()
    End Sub


VB
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim ts As New TimeSpan(30, 0, 0, 0)
        'tmr = New System.Windows.Forms.Timer
        'tmr.Interval = 1000
        'AddHandler tmr.Tick, AddressOf Ticker_Tick
        'MsgBox("Ok, Let's start")
        'tmr.Start()
        Me.Text = "0:0"
        Dim ts As New TimeSpan(30, 0, 0, 0)
        tmr = New System.Windows.Forms.Timer
        tmr.Interval = 1
        AddHandler tmr.Tick, AddressOf Ticker_Tick
    End Sub


Help Me pls, vital for me, Thankz a lot.
 
Share this answer
 
Please don't push 'answer' if you're not answering a question. Edit your post to ask more questions about the same thing. Also, this was a very simple task, you should probably buy a beginners book and work through it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900