1. 程式人生 > >winform開啟新窗體關閉舊窗體

winform開啟新窗體關閉舊窗體

專案需求,程式的主頁面不關閉,當點選流程頁面時候彈出新窗體,關閉舊窗體。利用委託實現了這個需求,直接上程式碼

舊窗體(Form3窗體)中程式碼:

private void button1_Click(object sender, EventArgs e)
        {
            //this.Hide();隱藏舊窗體
            //Form4 f = new Form4(FormClose);
            //f.ShowDialog();
            Thread th = new Thread(new ThreadStart(delegate
            {
                Application.Run(new Form4(FormClose));
            }));

            th.Start();
        }

        private void FormClose()
        {
            this.BeginInvoke(new MethodInvoker(delegate { this.Close(); }));
        }

新窗體(Form4窗體)中程式碼

    public Form4(Action act)
        {
            InitializeComponent();
            act();
        }