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

求Winform中倒计时手动设定时间代码?

求Winform中倒计时手动设定时间代码?
就是用Text手动输入时间,电脑开始在做倒计时操作,谢谢! --------------------编程问答-------------------- 这个还真不懂怎么说 --------------------编程问答-------------------- (1)可以用延时来实现 你从Text输入10,然后写个for循环,循环里面用Thread.Sleep(1000) 延时1秒,当i=10,执行10秒后你想要执行的东西
(2)用Timer控件,设置间隔1秒执行一次,如果输入10给个变量,每个1秒变量=变量-1,当变量=0,执行你要执行的代码
(3)期待高手给更好的思路 --------------------编程问答-------------------- 用Timer控件。。。。
间隔设置为1秒,每过一秒就把你输入的时间减少一点,知道时间过完。 --------------------编程问答-------------------- 网上有类似的代码,实现如下:

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

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
   private uint Remainder;
   private uint minute;
   private uint sencond;
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.Label label2;
   private System.Windows.Forms.Label label3;
   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.Label lbShow;
   private System.Windows.Forms.TextBox txtTime;
   private System.Windows.Forms.TextBox txtEnd;
   private System.Windows.Forms.TextBox txtStart;
   private System.Windows.Forms.Timer timer1;
   private System.Windows.Forms.Timer timer2;
   private System.ComponentModel.IContainer components;

   public Form1()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }

   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if (components != null) 
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }

   #region Windows 窗体设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>

/*省去自动生成代码*/
    #endregion

   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main() 
   {
    Application.Run(new Form1());
   }

   private void timer1_Tick(object sender, System.EventArgs e)
   {
    txtStart.Text=System.DateTime.Now.ToLongTimeString();
   }

   private void button1_Click(object sender, System.EventArgs e)
   {
    label1.Text="当前时间";
    timer1.Enabled=true;
    label2.Visible=false;
    txtEnd.Visible=false;
    lbShow.Text="";
    txtTime.Focus();
    txtTime.Text="";
    timer2.Enabled=false;
   }

   private void txtTime_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
   {
    if(e.KeyChar=='\r')
    {
     if(this.txtTime.Text=="")
      return;
     Remainder=Convert.ToUInt32(txtTime.Text)*60;
     label2.Visible=true;
     txtEnd.Visible=true;
     label1.Text="开始时间";
     timer1.Enabled=false;
     timer2.Enabled=true;
     this.button1.Focus();
    
    }
   }

   private void timer2_Tick(object sender, System.EventArgs e)
   {
    txtEnd.Text=System.DateTime.Now.ToLongTimeString();
   
    Remainder--;
    minute=Remainder/60;
    sencond=Remainder%60;
    if(sencond<10)
    {
     lbShow.Text="剩余时间:"+minute+"分0"+sencond+"秒";
    }
    else
    {
     lbShow.Text="剩余时间:"+minute+"分"+sencond+"秒";
    }
    if(Remainder==0)
    {
     timer2.Enabled=false;
     lbShow.Text+="---时间到。";
    }
   }
}
}



--------------------编程问答-------------------- int i = 30;
private void button2_Click(object sender, EventArgs e)
{
    timer1.Interval = 1000;
    timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
    textBox1.Text = i.ToString();
    if (i == 0) timer1.Stop();
    else i--;
}

--------------------编程问答-------------------- 楼上正解 --------------------编程问答-------------------- http://www.phome.asia/article/1009.html

看看这个对你有帮助没。 --------------------编程问答-------------------- 这个应该不难实现吧,关键是你在text里边输入的时间是什么样的单位,然后根据你所输入的数据做相应的处理,用timer控件挺好的,你在网上找找它的用法 --------------------编程问答-------------------- 我给出倒计时的思路:
首先获取你text1.text里的时间然后减去现在的时间=diff(相差的时间)
相差的时间/24*60*60*1000(即一天的时间)=离未来还有有几天
diff= 离未来还有有几天*(24*60*60*1000)
diff/60*60*1000(即还有小时)=离未来还有小时
diff=离未来还有几分钟*(60*60*1000)
diff/60*1000(即还有几秒种)=离未来还有分钟
diff=离未来还有几秒钟*(60*1000)=离未来还有几秒
 diff/60=离未来还有即微妙!


--------------------编程问答-------------------- 额~有几个字写错了。~,关系有点搞晕了!算了。!大概就这么个意思!你网上找一下把!
--------------------编程问答-------------------- 这个刚做过,以下是代码: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 倒计时1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int min, sec;
     
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                timer1.Enabled = true;
                label1.Text = "考试倒计时:";
                label2.Text = "";
                min = Convert.ToInt16(textBox1.Text);
                sec = Convert.ToInt16(textBox2.Text);
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                button1.Enabled = false;
                button2.Enabled = true;
            }
            catch
            {
                MessageBox.Show("输入格式有误!", "异常提示");
            }
          
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox2.Text = Convert.ToString(sec);
            textBox1.Text = Convert.ToString(min);
            sec--;
            if (sec < 0 && min > 0)
            {
                sec = 59;
                min--;
            }
           else
                if (sec <0 && min == 0)
                {
                    label1.Text = "时间用完!";
                    label2.Text = "考试结束,请教卷!";
                    timer1.Enabled = false;
                    button1.Enabled = true;
                    button2.Enabled = false;
                    textBox1.Enabled = true;
                    textBox2.Enabled = true;
                }
            
            

         
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            MessageBox.Show("你已交卷!","结束提示",MessageBoxButtons.OK,
                              MessageBoxIcon.Information);
                label2.Text = "你已经结束了考试,请交卷!";
                button2.Enabled = false;
                textBox1.Text = "0";
                textBox2.Text = "0";
               button1.Enabled = true;
               textBox1.Enabled = true;
        }  
    }
}
--------------------编程问答-------------------- 最好是在Timer 里面做
把它时间间隔设置成 1000 也就是1秒

在程序也可以做 
Thead.Sleep(1000)
这个会很好 
所以建议用Timer
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,