zoukankan      html  css  js  c++  java
  • C# 倒计时

    c#中有一个叫做timespan的数据类型,可以这样构造:

    TimeSpan ts = new TimeSpan(0, 45, 0);

    TimeSpan(hour,minute,second);

    然后拖进去一个timer,叫timer1

    timer1.Interval=1000; 

    设置一秒一个周期

    然后在timer的事件里这样写

    private void timer1_Tick(object sender, EventArgs e)
    {
    String str = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();
    label1.Text = str;//label1用来显示剩余的时间
    ts = ts.Subtract(new TimeSpan(0, 0, 1));//每隔一秒减去一秒
    if (ts.TotalSeconds < 0.0)//当倒计时完毕
    {
    timer1.Enabled = false;
    MessageBox.Show("考试时间到,系统将强行交卷");//提示时间到,下面可以加你想要的操作
    }
    }
    View Code

    界面设计

    namespace Countdown
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要修改
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.button1 = new System.Windows.Forms.Button();
                this.timer1 = new System.Windows.Forms.Timer(this.components);
                this.button2 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(179, 210);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(106, 33);
                this.button1.TabIndex = 0;
                this.button1.Text = "开启页面";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // timer1
                // 
                this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(55, 285);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(75, 23);
                this.button2.TabIndex = 1;
                this.button2.Text = "button2";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(486, 337);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Timer timer1;
            private System.Windows.Forms.Button button2;
        }
    }
    View Code

    界面后台

    using System;
    using System.Windows.Forms;
    
    namespace Countdown
    {
        public partial class Form1 : Form
        {
            private TimeSpan ts = new TimeSpan(0, 0, 0);
            private Form2 frm2 = null;
    
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                ts = new TimeSpan(0, 0, 50);
                timer1.Enabled = true;
    
                frm2 = new Form2();
                frm2.ShowDialog(this);
    
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                String str = ts.Seconds.ToString();
                frm2.current_time = str;
    
                ts = ts.Subtract(new TimeSpan(0, 0, 1));
                if (ts.TotalSeconds < 0.0 && !frm2.EnterPhone)
                {
                    timer1.Enabled = false;
                    frm2.Close();
                }
    
                if (frm2.EnterPhone)
                {
                    timer1.Enabled = false;
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Form4 f4 = new Form4();
                f4.ShowDialog();
            }
        }
    }
    View Code

    跳转界面的设计

    namespace Countdown
    {
        partial class Form4
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.label1 = new System.Windows.Forms.Label();
                this.timer1 = new System.Windows.Forms.Timer(this.components);
                this.SuspendLayout();
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Font = new System.Drawing.Font("宋体", 25F);
                this.label1.ForeColor = System.Drawing.Color.Red;
                this.label1.Location = new System.Drawing.Point(658, 13);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(66, 34);
                this.label1.TabIndex = 0;
                this.label1.Text = "99S";
                // 
                // timer1
                // 
                this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                // 
                // Form4
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(800, 450);
                this.Controls.Add(this.label1);
                this.Name = "Form4";
                this.Text = "Form4";
                this.Load += new System.EventHandler(this.Form4_Load);
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Timer timer1;
        }
    }
    View Code

    跳转界面的后台

    using System;
    using System.Windows.Forms;
    
    namespace Countdown
    {
        public partial class Form4 : Form
        {
    
            private TimeSpan ts = new TimeSpan(0, 0, 0);
            public Form4()
            {
                InitializeComponent();
            }
    
            private void Form4_Load(object sender, EventArgs e)
            {
    
                timer1.Enabled = true;
                timer1.Start();
    
                ts = new TimeSpan(0, 1, 39);
    
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                String str = ts.TotalSeconds.ToString();//ts.Seconds.ToString();
                ts = ts.Subtract(new TimeSpan(0, 0, 1));
                label1.Text = str;
                if (ts.TotalSeconds < 0.0 )
                {
                    timer1.Enabled = false;
                    this.Close();
                }
    
            }
        }
    }
    View Code
  • 相关阅读:
    Log4net实例(转自http://zjuoliver.blog.163.com/blog/static/5101920084299524443/)
    不同数据库获取新增加的主键值
    asp.net中的ALERT类
    Log4net操作指南(转自http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html)
    阅读器关闭时尝试调用Read无效
    LINQ中文教程LINQ初体验之LINQ to Object
    vs2010设置默认浏览器
    附加数据库时出现错误解决办法
    oracle安装后,第一次登陆的步骤
    已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
  • 原文地址:https://www.cnblogs.com/aijiao/p/10763723.html
Copyright © 2011-2022 走看看