Design part
Today we are going to something new about timer control.Timer control is very interesting and so easy. so don't be afraid.
You can show on screen 00:00:00. It is label and 3 button control and last one is used timer control. Timer control is not show on run-time program, it control show on below form.
Now Design phase is ready and discuss about coding part.
Coding part -
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 timerprogram
{
public partial class Form1 : Form
{
int hr, min, sec; // declare variable
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = hr.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
}
private void btnstart_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void btnpause_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void btnreset_Click(object sender, EventArgs e)
{
timer1.Stop();
hr = 0;
min = 0;
sec = 0;
label1.Text = hr.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
}
private void timer1_Tick(object sender, EventArgs e) //important timer used tick event.
{
if (sec < 59)
{
sec = sec + 1;
}
else
{
sec = 0;
if (min < 59)
{
min = min + 1;
}
else
{
min = 0;
hr = hr + 1;
}
}
label1.Text = hr.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
}
}
}

Comments
Post a Comment