Go to DotNetFunda.com
 Online : 1257 |  Welcome, Guest!   Login
 
Home > Articles > Windows Forms > StopWatch Application using C#

Submit Article | Articles Home | Search Articles |

StopWatch Application using C#

1 vote(s)
Rating: 5 out of 5
red flag  Posted on: 6/4/2010 6:19:27 AM by Muhilan | Views: 2567 | Category: Windows Forms | Level: Beginner


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

Download


 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.


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Muhilan

Latest Articles

About Muhil an

Experience:3 year(s)
Home page:
Member since:Monday, November 30, 2009
Level:Starter
Status: [Member]
Biography:working as an IT System Analyst

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 3:40:34 AM