0
点赞
收藏
分享

微信扫一扫

winform定时器功能

J简文 2022-04-24 阅读 123
winformc#

效果图

代码

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 winform定时器功能
{
    public partial class 时间器 : Form
    {
        public 时间器()
        {
            InitializeComponent();
        }

        private System.Windows.Forms.Timer tm时间;
        public void HandleTime(Object sender, EventArgs e)
        {
            //txt姓名.asText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            txt时间.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ;         
            
        }
        private void 时间器_Load(object sender, EventArgs e)
        {
            //创建定时器
            tm时间 = new System.Windows.Forms.Timer();
            tm时间.Tick += new EventHandler(HandleTime);
            tm时间.Interval = 1000;//定时1秒。
            tm时间.Enabled = true;
            //开启定时器
            tm时间.Start();
        }

        private void btn开始_Click(object sender, EventArgs e)
        {
            if (!tm时间.Enabled) 
            {

                tm时间.Start(); 
            }
            else {
                MessageBox.Show("已经开始了"); 
            }

        }

        private void btn结束_Click(object sender, EventArgs e)
        {
            if (tm时间.Enabled) 
            { 
            	tm时间.Stop(); 
            }
            else 
            {
                MessageBox.Show("已经停止了"); 
            }
        }

        private void 时间器_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tm时间.Enabled) tm时间.Stop();
        }
    }
}

举报

相关推荐

0 条评论