C# 如何用计时器Timer控件实现停留几秒再做切换窗体的操作

C# Timer用法及实例详解

关于C# Timer类  在C#里关于定时器类就有3个

C# Timer使用的方法1.定义在System.Windows.Forms里

C# Timer使用的方法2.定义在System.Threading.Timer类里  "

C# Timer使用的方法3.定义在System.Timers.Timer类里

◆System.Windows.Forms.Timer

应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API  SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console  Application(控制台应用程序)无法使用。   
  
◆System.Timers.Timer

和System.Threading.Timer非常类似,它们是通过.NET  Thread  Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。

◆System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。

C# Timer用法实例

使用System.Timers.Timer类


1

2

3

4

5

6

7

8

9

System.Timers.Timer t = new System.Timers.Timer(10000);   //实例化Timer类,设置间隔时间为10000毫秒;   

    t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;   

    t.AutoReset = true;   //设置是执行一次(false)还是一直执行(true);   

    t.Enabled = true;     //是否执行System.Timers.Timer.Elapsed事件;   

    

    public void theout(object source, System.Timers.ElapsedEventArgs e)   

     {   

        MessageBox.Show("OK!");   

     }

本回答由电脑网络分类达人 汪雷 推荐

评论

0 0

xiangjuan314

采纳率:95% 来自团队:电脑网络行家 擅长: 佛山市 烦恼 电脑/网络 生活 文化/艺术

其他回答


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

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 WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        int a = 0;

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            timer1.Enabled = true;

        }

        

        private void timer1_Tick(object sender, EventArgs e)

        {

            a += 1;

            progressBar1.Value = a;

            

            if (a == 5) {

                Form2 frm = new Form2();

                timer1.Enabled = false;

                a = 0;

                frm.Show();

                this.Hide ();

             }

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            progressBar1.Maximum = 5;

            progressBar1.Minimum =0;

            progressBar1.Value = 0;

            timer1.Interval = 1000;

        }

    }

}

时间: 2024-08-24 05:16:37

C# 如何用计时器Timer控件实现停留几秒再做切换窗体的操作的相关文章

timer控件、三级联动

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 timer控件 { public partial class Form1

C# windows服务:C#windows服务中的Timer控件的使用

C# windows服务程序中的Timer控件的使用问题是如何解决的呢? 今天研究了下C# windows服务程序中的Timer控件的使用的写法. 我们在建立一个C# windows服务程序后,要用到计时器的话一般都会想到Timer控件,于是到工具器把Timer控件拉过来,双击写事件,设置时间 但服务一跑起来,唉,怎么回事,时间到了怎么没有执行事件呢? 后来,我找很多资料才知道,这个Timer控件是winForm下的,不能用在C# windows服务程序中. 怎么办呢....... 打开"工具箱

WinForm Timer控件,三级联动[省,市,区]

Timer控件: 组件中的最后一个控件,功能是可以根据用户自定义的时间间隔来触发时间,不会印象窗体本身的其他事件进行. 属性: Enable  设置控件是否启用 Interval  设置事件的频率,以毫秒为单位 事件只有一个:Tick事件 例:使用timer控件获取当前时间并即时变动 private void timer1_Tick(object sender, EventArgs e) { label1.Text = DateTime.Now.ToString("yyyy年MM月dd日hh时m

WinForm用户控件、动态创建添加控件、timer控件--2016年12月12日

好文要顶 关注我 收藏该文 徐淳 关注 - 1 粉丝 - 3 0 0 用户控件: 通过布局将多个控件整合为一个控件,根据自己的需要进行修改,可对用户控件内的所有控件及控件属性进行修改使用 动态创建添加控件: 1 //定义控件类型 2 Button btn = new Button(); 3 //控件名称……等属性,也可以直接绑定各种事件 4 btn.Name = "mybutton" + i.ToString(); 5 //添加到窗体 this 可以替换为 容器控件 6 this.Co

无边框窗体 timer控件

一.无边框窗体1.控制按钮如何制作 MouseEnter-鼠标移入的时候发生的事件 pictureBox1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\..\\..\\images\\btn_close_highlight.png"); MouseLeave-鼠标移出的时候发生的事件 pictureBox1.BackgroundImage = Image.FromFile(Application.Start

IsMdicontainer新窗体被父容器包括的设置、Timer控件

IsMdicontainer的设置 这是对于整个窗体的设置,将一个窗体的IsMdicontainer设置为true之后,再打开新窗体便可以让新窗体被父容器包括在内. 操作方法: 1)先建立一个子窗体Child.cs 2)在父容器的按钮里写代码,打开子窗体 Child c = new Child(); //判断窗体是否已经打开,以控制其只能打开一个 int count = 0; foreach (Form f in this.MdiChildren) { if (f.Name == c.Name)

WinForm timer控件

timer 控件:按用户定义的时间间隔引发的事件 属性: Enabled   是否启用:  Interval    事件发生的事件间隔,单位是毫秒 事件只有一个:Tick    事件经过指定的时间间隔发生 打开一个窗口,获取同步时间(精确到秒) 首先在构造函数中设置初始状态 public Form1() { InitializeComponent(); label3.Text = DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒"); 其次设置在T

winform用户控件、动态创建添加控件、timer控件、控件联动

用户控件:(1) 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗体中添加, 将其实例化,然后添加到想要添加的容器的Control集合中. 动态创建添加控件: 配合上面的用户控件,实现类似QQ界面的打开自动加载好友昵称和签名 public Form1() { InitializeComponent(); //将当前登陆的账号的全部好友信息取出来 List<A

如何利用C# timer控件实现查看当前系统时间

运行环境:Microsoft Visio Studio 2015 控件说明:1.timer 2.button(一键获取当前系统时间!) 3.label(显示时间) 实现的效果如下: 时间跟随系统时间滚动输出 完整源代码: 1 using System; 2 using System.Windows.Forms; 3 4 namespace EXP3_53_GetTime 5 { 6 public partial class Form1 : Form 7 { 8 9 public Form1()