设为首页 加入收藏

TOP

倒计时功能实现(一)
2019-09-17 18:32:59 】 浏览:49
Tags:倒计时 功能 实现
 1 //Program.cs
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using System.Windows.Forms;
 7 
 8 namespace TimerExc01
 9 {
10     static class Program
11     {
12         /// <summary>
13         /// 应用程序的主入口点。
14         /// </summary>
15         [STAThread]
16         static void Main()
17         {
18             Application.EnableVisualStyles();
19             Application.SetCompatibleTextRenderingDefault(false);
20             Application.Run(new Form1());
21         }
22     }
23 }
  1 //Form1.cs
  2 using System;
  3 using System.Collections.Generic;
  4 using System.ComponentModel;
  5 using System.Data;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
 11 
 12 namespace TimerExc01
 13 {
 14     public partial class Form1 : Form
 15     {
 16         private int time = 0;
 17         private int FirstSecond = 0;//启动程序时的时间
 18         private int FirstHour = 0;
 19         private int FirstMinute = 0;
 20 
 21         public Form1()
 22         {
 23             InitializeComponent();
 24         }
 25 
 26         private void tmrCountTime_Tick(object sender, EventArgs e)
 27         {
 28             time++;
 29             int currentHour = time / 3600;
 30             int currentMinu = (time % 3600) / 60;
 31             int currentSecond = (time % 3600) % 60;
 32             this.tbxTime.Text = currentHour + "" + currentMinu + "" + currentSecond + "";
 33         }
 34 
 35         private void btnStart_Click(object sender, EventArgs e)
 36         {
 37             this.tmrCountTime.Enabled = true;
 38         }
 39 
 40         private void btnStop_Click(object sender, EventArgs e)
 41         {
 42             time = 0;
 43             this.tmrCountTime.Enabled = false;
 44         }
 45 
 46         #region 界面加载
 47         private void Form1_Load(object sender, EventArgs e)
 48         {
 49             DateTime datetime = System.DateTime.Now;
 50             this.numHour.Value = datetime.Hour;
 51             this.numMinu.Value = datetime.Minute;
 52             this.numSecond.Value = datetime.Second;
 53             FirstSecond = datetime.Second;//记录下最初的秒数
 54             FirstMinute = datetime.Minute;
 55             FirstHour = datetime.Hour;
 56             tmrCurrentTime.Enabled = true;
 57         }
 58         #endregion
 59 
 60         #region 定时器更新当前系统时间
 61         int countSecond = 0;
 62         private void tmrCurrentTime_Tick(object sender, EventArgs e)
 63         {
 64             int currentMinu = 0; int currentHour = 0;
 65             countSecond += 1;
 66             //最初的秒加上定时器走过的秒
 67             int PastSumAllSecond = FirstSecond + countSecond;
 68 
 69             //求小时数
 70             int hour = PastSumAllSecond / 3600;
 71 
 72             int leftSecond = PastSumAllSecond % 3600;
 73 
 74             //求分钟数
 75             int minute = leftSecond / 60;
 76 
 77             //求秒数
 78             int Second = leftSecond % 60;
 79 
 80 
 81             int resulSecond = FirstSecond + Second;
 82 
 83             //if (resulSecond >= 60)
 84             //{
 85             //    this.numSecond.Value = resulSecond % 60;
 86             //}
 87             //else
 88             //{
 89             this.numSecond.Value = Second;
 90             //}
 91             currentMinu  = FirstMinute + minute;
 92 
 93             if (currentMinu > 60)
 94             {
 95                 this.numMinu.Value = currentMinu % 60;
 96 
 97             }else if(currentMinu==60)
 98             {
 99                 FirstHour++;
100                 //currentHour = FirstHour + hour + 1;
101                 this.numMinu.Value = 0;
102             }
103             else
104             {
105                 currentMinu = FirstMinute + minute;
106                 this.numMinu.Value = currentMinu;
107             }
108 
109             
110             //更新界面小时,分钟,秒
111             if (currentHour > 24)
112             {
113                 this.numHour.Value = currentHour % 24;
114             }else if(currentHour==24)
115             {
116                 this.numHour.Value = 0;
117             }
118             else
119             {
120                 currentHour = FirstHour + hour;
121                 this.numHour.Value = currentHour;
122             }
123         }
124         #endreg
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇学习Linq之前必须要了解的扩展方法 下一篇.Net WebApi接口Swagger集成简单..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目