Previous topicNext topic
Help > 开发指南 > 窗体开发 > 控件参考 > 控件参考 > 其他 >
Timer(计时器)

计时器有时候会在我们的窗体开发中使用到,主要是用来在固定间隔时间内重复地做一些简单的动作。好处是Timer控件的事件不是异步的,我们可以在事件中更新窗体中的控件属性。

属性

属性名称 分类 描述
Enabled 基本 获取或设置计时器是否启动运行。
Interval 基本 获取或设置在相对于上一次发生的 Tick 事件引发 Tick 事件之前的时间(以毫秒为单位)。

方法

方法名称 分类 描述
Start 基本 启动计时器。
Stop 基本 停止计时器。

下面我们就以一个示例来演示一下如何使用Timer控件。

第一步,先按照下面的布局创建控件

第二步,设置Timer控件的属性,将Interval(促发计时器事件的时间间隔)设置为1000毫秒,设置“可用”属性为True,来启动计时器。

第三步:在计时器和命令按钮上写代码。

Vb.Net
Imports System
Imports System.Text
Imports System.IO
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Drawing.Text
Imports System.Diagnostics
Imports System.Collections
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Collections.Specialized
Imports System.Windows.Forms
Imports System.Reflection
Imports Microsoft.CSharp
Imports Microsoft.VisualBasic
Imports sanMuSoft.Utility
Imports sanMuSoft.Data.Pivot
Imports System.Linq
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Xml
Imports System.Data.Common
Imports System.Net.Http
Imports C1.C1Excel
Imports C1.C1Zip
Imports C1.Win.C1Command
Imports C1.Win.C1FlexGrid
Imports C1.Win.C1Input
Imports C1.Win.C1Ribbon
Imports C1.Win.C1Themes
Imports sanMuSoft.CS.Framework
Imports sanMuSoft.CS.Framework.Editor
Imports sanMuSoft.CS.Framework.FormDesigner
Imports sanMuSoft.CS.Framework.DropDownForms
Imports sanMuSoft.CS.WinForm
Imports sanMuSoft.CS.WinForm.Editor
Imports sanMuSoft.CS.WinForm.Controls
Imports sanMuSoft.CS.WinForm.Controls.Grid
Imports sanMuSoft.CS.WinForm.Controls.BoxControls
Imports sanMuSoft.CS.Workflow
Imports sanMuSoft.CS.Report
Imports sanMuSoft.Data
Imports sanMuSoft.Data.TableBuilder
Imports sanMuSoft.CS.ShareFunc
Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json
Imports Aliyun.OSS

Namespace FormEvents
    Public Class Form6bdd3d1a3756460bbd85023534d04a3f
        Inherits FormEventsBase
        
        Public Sub Timer1_Tick(sender As Object,e As  System.EventArgs)
            '每过1秒钟就重新赋值一下当前时间
            Dim lblTime As System.Windows.Forms.Label=Me.SmForm.ControlDictionary()("lblTime")
            lblTime.Text=DateTime.Now
        End Sub
        
        Public Sub TimerHelp_Load(sender As Object,e As  System.EventArgs)
            '为了在计时器在启动之前就能正常显示时间,需要在窗体加载时也要先赋值一下当前时间
            Dim lblTime As System.Windows.Forms.Label=Me.SmForm.ControlDictionary()("lblTime")
            lblTime.Text=DateTime.Now
        End Sub
        
        Public Sub btnStart_Click(sender As Object,e As  System.EventArgs)
            Dim Timer1 As System.Windows.Forms.Timer=Me.SmForm.Components.Components("Timer1")
            Timer1.Start()
        End Sub
        
        Public Sub btnStop_Click(sender As Object,e As  System.EventArgs)
            Dim Timer1 As System.Windows.Forms.Timer=Me.SmForm.Components.Components("Timer1")
            Timer1.Stop()
        End Sub
        
    End Class
End Namespace

C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Collections;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.Windows.Forms;
using Microsoft.CSharp;
using sanMuSoft.Utility;
using sanMuSoft.Data.Pivot;
using System.Threading;
using System.Xml;
using System.Data.Common;
using System.Net.Http;
using C1.C1Excel;
using C1.C1Zip;
using C1.Win.C1Command;
using C1.Win.C1FlexGrid;
using C1.Win.C1Input;
using C1.Win.C1Ribbon;
using C1.Win.C1Themes;
using sanMuSoft.CS.Framework;
using sanMuSoft.CS.Framework.Editor;
using sanMuSoft.CS.Framework.FormDesigner;
using sanMuSoft.CS.Framework.DropDownForms;
using sanMuSoft.CS.WinForm;
using sanMuSoft.CS.WinForm.Editor;
using sanMuSoft.CS.WinForm.Controls;
using sanMuSoft.CS.WinForm.Controls.Grid;
using sanMuSoft.CS.WinForm.Controls.BoxControls;
using sanMuSoft.CS.Workflow;
using sanMuSoft.CS.Report;
using sanMuSoft.Data;
using sanMuSoft.Data.TableBuilder;
using sanMuSoft.CS.ShareFunc;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Aliyun.OSS;

namespace FormEvents
{
    public class Form6bdd3d1a3756460bbd85023534d04a3f : FormEventsBase
    {
        public void Timer1_Tick(object sender, System.EventArgs e)
        {
            // 每过1秒钟就重新赋值一下当前时间
            System.Windows.Forms.Label lblTime = this.SmForm.ControlDictionary()["lblTime"] as System.Windows.Forms.Label ;
            lblTime.Text = DateTime.Now;
        }

        public void TimerHelp_Load(object sender, System.EventArgs e)
        {
            // 为了在计时器在启动之前就能正常显示时间,需要在窗体加载时也要先赋值一下当前时间
            System.Windows.Forms.Label lblTime = this.SmForm.ControlDictionary()["lblTime"] as System.Windows.Forms.Label;
            lblTime.Text = DateTime.Now;
        }

        public void btnStart_Click(object sender, System.EventArgs e)
        {
            System.Windows.Forms.Timer Timer1 = this.SmForm.Components.Components["Timer1"] as System.Windows.Forms.Timer;
            Timer1.Start();
        }

        public void btnStop_Click(object sender, System.EventArgs e)
        {
            System.Windows.Forms.Timer Timer1 = this.SmForm.Components.Components["Timer1"] as System.Windows.Forms.Timer;
            Timer1.Stop();
        }
    }
}

这里注意一下,引用Timer这种组件和引用Label这种控件的方式是不太一样的。

最后我们运行窗体的效果如下,点击暂停可以让时间刷新停止,再次点击启动时间又会继续跳动更新。