User Id :    Password :      New Member   Forgot Password  
 
Creating Countdown function in C sharp
Description Here I am going to give a function in c# that display the countdown in a label control.   No. of Views     1291
  Rating     0
Author Sumit Gupta   Posted On     08 Jul 2011
Tags C#    

Sample Code   Download Code

You can call this function from a thread or from a timer control with the frequency of 1 second. Before calling this function make sure to set the initial value of the label control for example lblTimer to 01:30:00

So here is the code-

private void CountDown()
    {
        try
        {
            string[] Duration = lblTimer.Text.Split(new char[] { ':' });
            string seconds = Duration[2];
            string minutes = Duration[1];
            string hours = Duration[0];
            seconds = (Convert.ToInt32(seconds) - 1).ToString();
            if (Convert.ToInt32(seconds) < 10 && Convert.ToInt32(seconds) >0)
            {
                seconds = "0" + Convert.ToInt32(seconds);
            }
            if (Convert.ToInt32(seconds) == -1)
            {
                seconds = "59";
                minutes = (Convert.ToInt32(minutes) - 1).ToString();
                if (Convert.ToInt32(minutes) < 10 && Convert.ToInt32(minutes) >0)
                {
                    minutes = "0" + Convert.ToInt32(minutes);
                }
                if (Convert.ToInt32(minutes) == -1)
                {
                    minutes = "59";
                    hours = (Convert.ToInt32(hours) - 1).ToString();
                }
                if (Convert.ToInt32(hours) < 10)
                {
                    hours = "0" + Convert.ToInt32(hours);
                }
            }
            lblTimer.Text = hours + ":" + minutes + ":" + seconds;
            if (lblTimer.Text == "00:00:00")
            {
                // do the required action on completion of coundown
            }
        }
        catch (Exception ex)
        {
           throw ex;
        }
    }

Happy coding...

 

About Author

About Author I am Sumit Gupta working in 3 Pillar Global Pvt. Ltd as Module Lead. I have 7+ year of experience in .Net technologies. I love to explore new technologies and write technical article. Sumit Gupta
No Photo
 
Country India
Company 3 Pillar Global Pvt. Ltd.
Home Page http://www.facebook.com/sumitgupta1225

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Annonymous on 02 Sep 2011 at 12:19 PM
 
TYVM you've solved all my prolbmes
 
 
   
Write your comment here.
Comment
Verification Code