C# 第六章『交互式图形界面』◆第3节:Form窗体—简单例子(2)

        工具箱—组件—Timer

        属性—事件—Tick—每当经过指定的时间间隔时发生。

在这里一定要先激活

        案例一:利用Timer弹窗

        private void timer1_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("中病毒了");
        }

        案例二:利用Timer字符循环移动

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 _20220725_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1); 
        }
    }
}

        案例三:利用Timer闹钟

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Windows.Forms;

namespace _20220725_4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();

            if (DateTime.Now.Hour == 22 && DateTime.Now.Minute == 35 && DateTime.Now.Second == 01)
            {
                MessageBox.Show("aaaaaaaa");
                SoundPlayer sp = new SoundPlayer();
                sp.SoundLocation = @"";
                
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_45336030/article/details/125984390