当前位置:编程学习 > C#/ASP.NET >>

c#倒计时代码

用label显示倒计时,比如显示60秒,
60,59,58,57.。。
60秒之后自动退出程序
该怎么做啊?
希望高手来帮忙! --------------------编程问答-------------------- 用timer --------------------编程问答-------------------- 在窗体中添加一个Timer和一个Label   
    
  int   time=10;   
  private   void   Form1_Load(object   sender,   System.EventArgs   e)   
  {   
          this.timer1.Interval   =   1000;   
          this.timer1.Tick   +=   new   System.EventHandler(this.timer1_Tick);   
          this.timer1.Start();   
  }   
  private   void   timer1_Tick(object   sender,   System.EventArgs   e)   
  {   
  if(time>-1)   
  {   
  time--;   
  this.label1.Text=time.ToString();   
  this.label1.Refresh();   
  }   
  else   
  {   
  timer1.Stop();   
  }   
  } --------------------编程问答-------------------- 以上,也是我刚找的,给你个参考呵呵  --------------------编程问答-------------------- 为什么是2秒2秒减得。。 --------------------编程问答-------------------- timer事件里

            int tmp = Convert.ToInt32(this.label1.Text);            
            tmp--;

            if (tmp == 0)
            {
                this.label1.Text = "时间到!!";
                timer.Stop();
                return;
            }

            this.label1.Text = Convert.ToString(tmp);
--------------------编程问答-------------------- 呵, 前几天刚写过一个, 给你看看:

        private DateTime dtExam = DateTime.Parse("2009-07-05 08:30:00");

        private void timerCD_Tick(object sender, EventArgs e)
        {
            if (this.dtExam < DateTime.Now)
            {
                this.timerCD.Enabled = false;
                this.Close();
            }
            else
            {
                RefreshTime();
            }
        }

        private void RefreshTime()
        {
            TimeSpan ts = this.dtExam - DateTime.Now;
            this.labDays.Text = ts.Days.ToString().PadLeft(2, '0') + "天";
            this.labTimes.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" +
                ts.Minutes.ToString().PadLeft(2, '0') + ":" +
                ts.Seconds.ToString().PadLeft(2, '0');
        }



        private void CountDown_Load(object sender, EventArgs e)
        {
            if (DateTime.Now > this.dtExam)
            {
                this.timerCD.Enabled = false;
                this.Close();
            }
            else
            {
                RefreshTime();
            } 
        }
    }

timerCD 控件的 Interval  值为  1000;   Enable 为 True; --------------------编程问答--------------------
引用 2 楼 moonzap 的回复:
在窗体中添加一个Timer和一个Label

   int   time=10;
   private   void   Form1_Load(object   sender,   System.EventArgs   e)
   {
           this.timer1.Interval   =   1000;
           this.timer1.Tick   +=   new   System.EventHandler(this.timer1_Tick);
           this.timer1.Start();
   }
   private   void   timer1_Tick(object   sender,   System.EventArgs   e)
   {
   if(time>-1)
   {
   time--;
   this.label1.Text=time.ToString();
   this.label1.Refresh();
   }
   else
   {
   timer1.Stop();
   }
   }


怎么设置在time=0时,过一分钟再从10循环开始呢?
谢谢! --------------------编程问答-------------------- 我说了 这个是给楼主参考用的 不是最后的答案 --------------------编程问答-------------------- Up! --------------------编程问答-------------------- 我刚自己写了一份 给你看看 也参考下

  private int   time=60; 
  private   void   Form1_Load(object   sender,   System.EventArgs   e) 
  { 
          this.label1.Text = string.empty;
          this.timer1.Interval   =   1000; 
          this.timer1.Tick   +=   new   System.EventHandler(this.timer1_Tick); 
          this.timer1.Start(); 
  } 
  private   void   timer1_Tick(object   sender,   System.EventArgs   e) 
  { 
    if(time>0) { 
       this.label1.Text=time.ToString(); 
       time--; 
      } 
  else 
       { 
          timer1.Enabled = false;
          timer1.Stop(); 
          return;
       } 
  } 

测试过了 好用 呵呵 --------------------编程问答-------------------- public static void Main()
    {
        int time = 100;
        DateTime date = new DateTime();
        date = DateTime.Now;
        int k = date.Second;
        while (true)
        {
            date = DateTime.Now;
            int m = date.Second;

            if (m != k)
            {
                Console.WriteLine(time--);
                k = m;
            }
        }

    } --------------------编程问答-------------------- 好用! --------------------编程问答--------------------
引用 10 楼 moonzap 的回复:
我刚自己写了一份 给你看看 也参考下

private int time=60;
private void Form1_Load(object sender, System.EventArgs e)
{
this.label1.Text = string.empty;
this.timer1.Interval = 1000;
this.timer1.Tick += new Syste……
推荐
--------------------编程问答-------------------- MARK --------------------编程问答-------------------- 楼主大概不知道有Timer这么个东西............ --------------------编程问答-------------------- 用timer,间隔设为1000毫秒
--------------------编程问答-------------------- --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace random
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private int time = 60;
        private void Form2_Load(object sender, EventArgs e)
        {
            this.timer1.Interval = 1000;
            this.timer1.Tick+=new EventHandler(timer1_Tick);
            timer1.Start();
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (time > 0)
            {
                
                label1.Text = time.ToString();
                time--;
               // label1.Refresh();
            }
            else 
            {
                timer1.Stop();
            }
            while (time == 0) 
            {
                timer1.Stop();
                time = 10;
            }
        }
    }
}
--------------------编程问答--------------------
引用 4 楼 cosker7788 的回复:
为什么是2秒2秒减得。。

this.timer1.Tick+=new EventHandler(timer1_Tick);

你可以把这句代码去掉就可以一秒一秒减少
EventHandler 是一个预定义的委托,专用于表示不生成数据的事件的事件处理程序方法。如果事件生成数据,则必须提供自己的自定义事件数据类型,并且必须要么创建一个委托,其中第二个参数的类型为自定义类型,要么使用泛型 EventHandler 委托类并用自定义类型替代泛型类型参数。

若要将事件与处理事件的方法关联,请向事件添加委托的实例。除非移除了该委托,否则每当发生该事件时就调用事件处理程序。 

它就委托两次,所以都是两秒两秒的减少

--------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace daojishi
{
    public partial class Form1 : Form
    {
        int time = 60;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
                    }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (time > 0)
            {
                time--;
               label1.Text = time.ToString ();
               
            }
            else
            {
                timer1.Stop();
            } 

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }
} --------------------编程问答-------------------- 呵呵顶一下
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,