Click here to Skip to main content
12,886,856 members (36,477 online)
Click here to Skip to main content
Add your own
alternative version


56 bookmarked
Posted 30 Jan 2004

Tiny StopWatch Application

, 30 Jan 2004
Rate this:
Please Sign up or sign in to vote.
A tiny app for timing things to 1/2 a second or so. My entry for the smallest useful app contest.

Sample Image - stopwatchapp_image.jpg


The code for this Windows application is so compact, it is presented here inline. Belying its small size, however, the app actually solves a real-world problem, namely the need to measure events of a few minutes' duration to about a half-second granularity. How long does Photoshop take to launch? How long have I been on the phone? "I'll bring your stapler back in five minutes." Yeah, right. The Radio Shack Deluxe Sports Stopwatch (63-5017) costs $19.99.


This is my entry in the imaginary "smallest useful app" contest--the entire app requires just 16 lines of user code. Thanks to .NET, the exe file is just 12.5k.

The Source Code

As they do in Microsoft Knowledgebase How-To articles, here are the step-by-step instructions:

  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"
        startTime = DateTime.Now()
        Me.Text = "Click to Stop"
    End If
End Sub

To use the stopwatch, click inside its window. To stop it, click again. Clicking again will reset the stopwatch to 0:0.0 and start it counting again. For fancy effects like lap timing, just launch several copies of the StopWatch app at once.

Points of Interest

There is nothing very interesting here except how an app so simple and small can be surprisingly useful.


01/31/04 AD: Initial


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alastair Dallas
Web Developer
United States United States
Alastair Dallas is a programmer, systems architect, and information designer with over 20 years experience at companies including Ashton-Tate, Lotus, Netscape, Documentum, and a wide variety of startups. Languages include C#, C, C++, Java, JavaScript, Perl, Python, and VB (in alphabetical order) as well as DHTML, XML, and related technologies. He has written 3 books, 2 on computer topics, and is President of Infospect Press.

You may also be interested in...

Comments and Discussions

Questionam little bit confused Pin
Member 1312353012-Apr-17 1:20
memberMember 1312353012-Apr-17 1:20 
GeneralMy vote of 3 Pin
mavrudebog16-Mar-12 7:06
membermavrudebog16-Mar-12 7:06 
GeneralDateTime Pin
rootjumper27-Mar-09 3:56
memberrootjumper27-Mar-09 3:56 
QuestionTimer Pin
rathna4code6-Mar-09 6:02
memberrathna4code6-Mar-09 6:02 
Questioninterval time Pin
hendry Gunawan3-Mar-09 17:43
memberhendry Gunawan3-Mar-09 17:43 
QuestionTime Interval Pin
hendry Gunawan3-Mar-09 17:36
memberhendry Gunawan3-Mar-09 17:36 
GeneralGood!! Pin
nma20200018-Mar-08 3:19
membernma20200018-Mar-08 3:19 
GeneralGOOD Pin
dubbele onzin13-Nov-07 19:10
memberdubbele onzin13-Nov-07 19:10 
GeneralThx Man It works!!!! Pin
dissanayake1-Oct-07 5:22
memberdissanayake1-Oct-07 5:22 
Generalthank you soo much Pin
Syed Ali hasan22-Aug-07 3:32
memberSyed Ali hasan22-Aug-07 3:32 
GeneralSmall suggestion: use Environment.TickCount Pin
nmitha4-Jan-07 10:50
membernmitha4-Jan-07 10:50 
If you replace DateTime.Now with Environment.TickCount, you avoid instantiating DateTime objects and you end up subtracting numbers (longs) instead of DateTimes. DateTime.Now has more overhead than you might think because it deals with things like culture information and time zones, and the Subtract method has to do a bit of work to get the result. Also, you never know, but Windows XP might decide to synchronize your clock with while the stopwatch is on...

GeneralCompact framework Pin
Philvis19955-May-06 4:07
memberPhilvis19955-May-06 4:07 
QuestionHelp? Pin
kevinbaum15-Mar-06 3:20
memberkevinbaum15-Mar-06 3:20 
GeneralThanks Pin
arpita200620-Feb-06 21:17
memberarpita200620-Feb-06 21:17 
QuestionHow do I get the Timer to start from Zero instead of the current Time? Pin
ensuvari8-Nov-05 3:48
memberensuvari8-Nov-05 3:48 
GeneralThanks Pin
vbtango20-Oct-05 16:52
membervbtango20-Oct-05 16:52 
QuestionContinue the counter instead of resetting? Pin
piniata21-Apr-05 21:55
memberpiniata21-Apr-05 21:55 
GeneralHelped me out a treat Pin
Vicbelly17-Mar-05 8:30
memberVicbelly17-Mar-05 8:30 
GeneralCouldn't get Stopwatch to work Pin
Greg S.22-Oct-04 3:54
memberGreg S.22-Oct-04 3:54 
GeneralRe: Couldn't get Stopwatch to work Pin
Greg S.25-Oct-04 12:40
memberGreg S.25-Oct-04 12:40 
GeneralA good starting point Pin
Michael Mogensen20-Mar-04 23:56
memberMichael Mogensen20-Mar-04 23:56 
AnswerRe: A good starting point Pin
ManaClear2-Mar-06 14:49
memberManaClear2-Mar-06 14:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 31 Jan 2004
Article Copyright 2004 by Alastair Dallas
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid