C# Timer定时器
Timer定时器定时器主要用到的就是Timer的Tick事件,另外还要设置时间间隔:
下面这个实力演示了每隔一秒,picturebox中的图片来回切换,每隔一秒,文本框中显示当前时间。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp2
{public partial class Form6 : Form{public Form6(){InitializeComponent();}string path1 = @"E:\朱维照片\创作照\1.jpg";string path2= @"E:\朱维照片\创作照\2.jpg";bool flag = true;private void button1_Click(object sender, EventArgs e){timer1.Interval = 1000;timer1.Start();}private void timer1_Tick(object sender, EventArgs e){textBox1.Text = DateTime.Now.ToString();if (flag){pictureBox1.Image = Image.FromFile(path1);pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;flag = false;}else{pictureBox1.Image = Image.FromFile(path2);pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;flag = true;}}private void button2_Click(object sender, EventArgs e){timer1.Stop();}}
}