2013/02/28

C#.Net A視窗切換B視窗

A Form的按鈕先宣告一個B Form物件;這個物件加入了FormClosed的事件,並且顯示B Form

那A Form就先隱藏起來,等到B Form關閉時會執行FormClosed事件,B Form的FormClosed事件是針對A Form視窗去使用的,所以就顯示出A Form視窗





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

        private void button_Click(object sender, EventArgs e)
        {
            B bForm = new B();
            bForm.FormClosed += new FormClosedEventHandler(B_FormClosed);
            bForm.Show();
            this.Hide();
        }

        private void B_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Show();
        }
    }
}

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

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}





參考資料:
http://createps.pixnet.net/blog/post/32345801