Your Ads Here

Monday 6 February 2012

Make a countdown timer in C#.NET


This example shows how to make a countdown timer in C#.NET
Keywords: countdown timer, timer, alarm, CSharp.NET, C#.NET

If you enter a duration in the format h:mm:ss and click the button, the following code converts your text into a TimeSpan and adds it to the current time. It saves the result in m_StopTime and enables the timer1 timer.

private void button1_Click(object sender, EventArgs e)
        {
            TimeSpan duration = TimeSpan.Parse(textBox1.Text);
            m_StopTime = DateTime.Now.Add(duration);
            
           timer1.Enabled = true;
        }

When the timer fires, it calculates the difference between the current time and the desired stop time and displays the time remaining in the label lblRemaining. Then if the total seconds remaining is zero, the program maximizes its form and makes its form topmost so it appears above all other forms, hopefully attracting your attention.


private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan remaining = m_StopTime.Subtract(DateTime.Now);
            remaining = new TimeSpan(remaining.Hours, remaining.Minutes, remaining.Seconds);
            if (remaining.TotalSeconds < 0)
                remaining = TimeSpan.Zero;

            label1.Text = remaining.ToString();

            if (remaining.TotalSeconds <= 0)
            {
                this.WindowState = FormWindowState.Maximized;
                this.TopMost = true;
                timer1.Enabled = false;
            }
        }

 
Here the full code.

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 testc
{

    public partial class Form1 : Form
    {
        System.DateTime m_StopTime = default(System.DateTime);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            TimeSpan duration = TimeSpan.Parse(textBox1.Text);
            m_StopTime = DateTime.Now.Add(duration);
            
           timer1.Enabled = true;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan remaining = m_StopTime.Subtract(DateTime.Now);
            remaining = new TimeSpan(remaining.Hours, remaining.Minutes,
                remaining.Seconds);
            if (remaining.TotalSeconds < 0)
                remaining = TimeSpan.Zero;

            label1.Text = remaining.ToString();

            if (remaining.TotalSeconds <= 0)
            {
                this.WindowState = FormWindowState.Maximized;
                this.TopMost = true;
                timer1.Enabled = false;
            }
        }
    }
}


4 comments:

  1. timer1 one is not working.

    ReplyDelete
  2. can u guide me how to implement countdown timer using webform(aspx) in asp.net using c#

    ReplyDelete
  3. its working properly

    ReplyDelete
  4. "The name 'timer1' does not exist in the current context"
    So, the project is not working because of this! 😐

    ReplyDelete