0

Hey frns I want to record the time that the application takes. I want that when i click on Start Button in label time starts from 00:00:00, & when we click on cancel button time resets to zero.
I include system.diagnostics.stopwatch

I just know start,stop & Reset methods are there.But i do not know how to use it,Can somebody frd me the coding............

4
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Naushad Malik
0
Dim stopWatchInstance as new System.Diagnostics.StopWatch()
stopWatchInstance.Start()
stopWatchInstance.Stop()
stopWatchInstance.Reset()
0

1. Start Microsoft Visual Studio .NET
2. On the File menu, point to New, and then click Project
3. Under Project Types, click Visual Basic Projects
4. Under Templates, click Windows Application. Form1 is created.
5. Change Form1's Properties: .Text="Click to Start", .FormBorderStyle="FixedToolWindow", .Size="192,64"
6. Place a new Label control anywhere on the form. Label1 is created.
7. Change Label1's Properties: .Text="StopWatch", .Dock="Fill", .Font="Georgia, 24pt", .TextAlign="MiddleCenter". Select foreground and background colors to taste (the example is .BackColor=Hot Track, .ForeColor=Menu).
8. Place a new Timer control on the form. Timer1 is created.
9. Right-click Form1, and then click View Code
10. Add the following code to the Form1 class:

Dim startTime As DateTime

    Private Sub Timer1_Tick(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim span As TimeSpan = DateTime.Now.Subtract(startTime)
        Label1.Text = span.Minutes.ToString & ":" & _
        span.Seconds.ToString & "." & span.Milliseconds
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles Label1.Click
        If (Timer1.Enabled) Then
            Me.Text = "Click to Re-start"
            Timer1.Stop()
        Else
            startTime = DateTime.Now()
            Me.Text = "Click to Stop"
            Timer1.Start()
        End If
    End Sub

    Private Sub Stopwatch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        startTime = DateTime.Now()
        Timer1.Start()
    End Sub

Author : Alastair Dallas

0
Public Class Form1
    Dim watch As New Stopwatch

    Private Sub btnString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnString.Click
        Timer1.Enabled = True
        watch.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = String.Format("{0}:{1}:{2}:{3}", watch.Elapsed.Hours.ToString, watch.Elapsed.Minutes.ToString, watch.Elapsed.Seconds.ToString, watch.Elapsed.Milliseconds.ToString)
    End Sub

    Private Sub btnStringBuilder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStringBuilder.Click
        Timer1.Enabled = False
        watch.Stop()
    End Sub
End Class

Edited by Nick Evan: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.