StopWatch Application using C#

Muhilan
Posted by in Windows Forms category on for Beginner level | Views : 40392 red flag
Rating: 4 out of 5  
 2 vote(s)

In this article explains you how to create Stopwatch Application using C#.


 Download source code for StopWatch Application using C#


Introduction


Create Stopwatch Application using C# with graphical form design.





Steps


1. Create a New project , Select Windows Application under project types , enter ApplicationForStopWatch 



2.  In Form1.cs include the Globalization , diagnostics and drawing2d namespace.

using System.Diagnostics;
using System.Globalization;
using System.Drawing.Drawing2D;
  Diagnostics name-space consists lot of classes for interacting system oriented functionality like system process, events and logs and Drawing2D consists lot of classes for drawing the vector graphics with two dimensional functionality.

3. Design the form 

   a) Display timer Laebl - lblDisplayTime
   b) Start Button - btnStart
   c) Stop Button  - btnStop
   d) Close label- lblClose
   e) Reset LinkButton - lnkReset
 

  Here i have design some different shape for Stopwatch application using this.region method

4. On Form_Load event write the below code

GraphicsPath gp = new GraphicsPath();

gp.AddArc(0, 0, 100, 200, 280, 90);
gp.AddArc(100, 0, 100, 200, 370, 90);
gp.AddArc(100, 100, 100, 200, 0, 90);
gp.AddArc(0, 100, 100, 200, 90, 90);

this.Region = new Region(gp);
    Region classes is used to define the area of the display surface , the area of the display we can specify at any types like straight, curves , rectangle , triangle , arc etc..

5. Drag and drop the Timer Control , set the interval = 50  and enabled=true (StopWatchTimer)

6.  Declare the below Stopwatch class in global declaration

 Stopwatch objStopWatch = new Stopwatch();
bool paused = false;
Stopwatch classes is used to measure the elapsed time. it have several method to start, stop, paused the elapsed time

7. In TimerControl(StopWatchTimer) Tick write the below code


if (objStopWatch.IsRunning)
{
TimeSpan objTimeSpan = TimeSpan.FromMilliseconds(objStopWatch.ElapsedMilliseconds);
lblDisplayTime.Text = String.Format(CultureInfo.CurrentCulture, "{0:00}:{1:00}:{2:00}.{3:00}", objTimeSpan.Hours, objTimeSpan.Minutes, objTimeSpan.Seconds, objTimeSpan.Milliseconds / 10);

if (paused)
{
paused = false;
}
}

    

TimeSpan class is used to get the individual hours, minutes , seconds based on stopwatch object ElapsedMilliseconds.

It will display the elapsed time.
8. Start the stopwatch class in click of start button event



 private void btnStart_Click(object sender, EventArgs e)
{
objStopWatch.Start();

}

9. Stop the stopwatch object in click of stop button event

 private void btnStop_Click(object sender, EventArgs e)
{
objStopWatch.Stop();
}

10. Reset the timer while click the Reset Button

           private void lnkReset_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{

lblDisplayTime.Text = "00:00:00.00";
objStopWatch.Reset();
}
11. Run (F5) output should be




Please feel free to send your feedback. Thank you.

Page copy protected against web site content infringement by Copyscape

About the Author

Muhilan
Full Name: Muhil an
Member Level:
Member Status: Member
Member Since: 11/30/2009 2:46:25 AM
Country: India


working as an IT System Analyst,tmuhilan at gmail com

Login to vote for this post.

Comments or Responses

Posted by: Shitalr on: 7/17/2013 | Points: 25
no use of Close label

Login to post response

Comment using Facebook(Author doesn't get notification)