C#里面的多窗体问题,有两个窗体Form1,Form2,准备在Form1上的BUTTON1点击Form2弹出来并同时关闭Form1,public  Form1 ZhuJieMian;public  Form2 GuYuan ;声明了两个窗体的量ZhuJieMian,GuYuan
private void Form1_Load(object sender, EventArgs e)
        {
             ZhuJieMian = new Form1();
             GuYuan = new Form2();
             ZhuJieMian.button1.Text = "显示 FORM2 并 关闭 FORM1";            
        }        private void button1_Click(object sender, EventArgs e)
        {
            GuYuan.Show();
            ZhuJieMian.Close();
        }
为什么弹出Form2的时候Form1关闭不掉啊!!!我看不出错在哪里?

解决方案 »

  1.   

    首先向要打开的窗体传入本窗体this,frmmain frm =new frmmain(this);frm .ShowDialog();
    在frmmain添加变量生命和构造函数,private frmmain form;
    public frmmain(frmmain  form)
    {
    InitializeComponent();
    this.form =form;
    this.form .Visible =false;
    }
    最后在closing事件中添加
    private void frmmain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    this.form.Visible=true;
    }
      

  2.   

    private void Form1_Load(object sender, EventArgs e)
            {
                this.button1.Text = "显示 FORM2 并 关闭 FORM1";            
            }        private void button1_Click(object sender, EventArgs e)
            {
               GuYuan = new Form2();  
               this.Close();
               GuYuan.Show();
            }
      

  3.   

    还忘了说一个问题,就是Form1不能是程序的入口点(即包含main函数的类).这样Form1关掉了Form2也会随之关掉.
      

  4.   

    Form1打開Form2的時候,Form1是不能關的,只能用Form1.Hide()這樣子!
      

  5.   

    你在form1中new了一个form1 ZhuJieMian
    你关闭是的ZhuJieMian,form1怎么能关闭的了呢?你要关闭form1就只能用this.close()