C# WinForm 提示框延遲自動關閉


有時候我們需要彈出個提示框然后讓它自己關閉,然而實際使用中的彈出框確實阻塞進程,網上貌似有一種另類的解決方式,大致思路是把彈出框放到另外的一個窗體上,直接貼代碼

主窗體

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), "執行成功!");
        }
    }
}

  

起到延遲作用的窗體

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();
        }
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM