c# timer program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace Countdown
{
public partial class Form1 : Form
{
// Follow the video to set up the rest of the project:
System.Timers.Timer t;
int h = 0, m = 0, s = 0, ms = 0;
public Form1()
{
InitializeComponent();
}
private void txtResult_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load_1(object sender, EventArgs e)
{
s = 1;
t = new System.Timers.Timer();
t.Interval = 10;
t.Elapsed += OnTimeEvent;
}
private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e)
{
Invoke(new Action(() =>
{
ms -= 1;
if (ms < 0)
{
ms = 60;
s -= 1;
}
if (s < 0)
{
s = 60;
m -= 1;
}
if (m < 0)
{
m = 60;
h -= 1;
}
if (h < 0)
{
h = 60;
}
if (ms == 0 && s == 0 && m == 0 && h == 0)
{
t.Stop();
}
txtResult.Text = string.Format("{0}:{1}:{2}:{3}", h.ToString().PadLeft(2, '0'), m.ToString().PadLeft(2, '0'), s.ToString().PadLeft(2, '0'), ms.ToString().PadLeft(2, '0'));
}));
}
private void btnStart_Click(object sender, EventArgs e)
{
t.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
t.Stop();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t.Stop();
Application.DoEvents();
}
}
}