每隔一段时间执行一次制定程序(如显示实时时间),这就需要用到Timer控件,记得刚开始学习的那会感觉很难、理解不了,现在回头来看,太简单了,哈哈!
Timer控件的主要属性:
Enable:Timer控件是否启用
Interval:事件的运行间隔时间
Timer控件的事件:
timer_Tick:事件间隔时进行的操作
实时时钟的简单实现:
新建Windows应用程序,在窗体中添加两个label(lblCurrentTime,lblTime),代码页面如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TimerTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timerTest_Tick(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private void Form1_Load(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}