I needed a 5 minute countdown for a project I was working on so I hacked this up, It does nothing more and nothing less. Figured I would post it while I was here to save someone else the trouble. (Not that it's in anyway difficult to do.)
5 minute count down timer
ddanbe commented: Nice! +6
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace CountDownTimers
{
public class Fivemintimer
{
Timer T = new Timer();
int Minutes = 5;
int Seconds = 0;
public Fivemintimer()
{
T.Interval = 1000;
T.Tick += new EventHandler(T_Tick);
}
public delegate void onTimerFinishedDelegate();
public event onTimerFinishedDelegate onTimerFinished;
public delegate void onTimerChanged(FIVETimerEventArgs args);
public event onTimerChanged onTimerTick;
void T_Tick(object sender, EventArgs e)
{
if (Minutes == 0 && Seconds == 0)
{
T.Stop();
if (onTimerFinished != null)
{
onTimerFinished();
}
}
else
{
if (Seconds == 0)
{
Seconds = 59;
Minutes--;
}
else
{
Seconds--;
}
}
if (onTimerTick != null)
{
onTimerTick(new FIVETimerEventArgs(Minutes, Seconds));
}
}
public void PlayPause()
{
if (T.Enabled)
{
T.Stop();
}
else
{
T.Start();
}
}
public void Reset()
{
Minutes = 5;
Seconds = 0;
}
public bool Enabled
{
get
{
return T.Enabled;
}
}
}
public class FIVETimerEventArgs : EventArgs
{
public string TimeLeft;
public FIVETimerEventArgs(int min, int sec)
{
string S;
if (sec < 10)
{
S = "0" + sec.ToString();
}
else
{
S = sec.ToString();
}
TimeLeft = min.ToString() + ":" + S;
}
}
}
ddanbe 2,724 Professional Procrastinator Featured Poster
anders.blomqvist2 0 Newbie Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
kplcjl 17 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.