• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#倒计时

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#WinForm用MenuStrip动态生成菜单并动态加载事件发布时间:2022-07-13
下一篇:
●用C#操作SQL数据库发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap