C# WinForm 提示框延迟自动关闭

https://www.cnblogs.com/luludongxu/p/8856956.html

有时候我们需要弹出个提示框然后让它自己关闭,然而实际使用中的弹出框确实阻塞进程,网上貌似有一种另类的解决方式,大致思路是把弹出框放到另外的一个窗体上,直接贴代码

主窗体

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

using System;

using System.Windows.Forms;

namespace WinForm

{

    public partial class MainForm : Form

    {

        public MainForm()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            //延迟800毫秒关闭的信息框

            MessageBox.Show(new DelayCloseForm(800), "执行成功!");

        }

    }

}

  

起到延迟作用的窗体

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

using System;

using System.ComponentModel;

using System.Windows.Forms;

namespace WinForm

{

    public partial class DelayCloseForm : Form

    {

        public DelayCloseForm(int interval = 500)

        {

            InitializeComponent();

            //计时器

            this.components = new Container();

            Timer timer1 = new Timer(this.components);

            timer1.Enabled = true;

            timer1.Interval = interval;

            timer1.Tick += timer1_Tick;

            timer1.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)

        {

            this.Close();

        }

    }

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值