Home > Codes, Sample Application, Tutorial for Beginners > Create StopWatch/Timer Using C#.net – Codes

Create StopWatch/Timer Using C#.net – Codes

With the help of following code, you can create a small timer/stopwatch application in c#

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace timer
{
public partial class Form1 : Form
{
int ms,s,m,h;
public Form1()
{
InitializeComponent();
ms = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
ms = ms + 1;
if (ms == 9)
{
ms = 0;
s = s + 1;
lblSecond.Text = s.ToString();
if (s == 59)
{
s = 0;
m = m + 1;
lblMin.Text = m.ToString();
if (m == 59)
{
m = 0;
h = h + 1;
lblHour.Text = h.ToString();
}
}
}
lblMsec.Text = ms.ToString();
}
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void lblReset_Click(object sender, EventArgs e)
{
ms = 0;
h = 0;
s = 0;
m = 0;
timer1.Enabled = false;
lblHour.Text = "0";
lblMin.Text = "0";
lblMsec.Text = "0";
lblSecond.Text = "0";
}
}
}

Post Comments Below

  1. No comments yet.
  1. No trackbacks yet.