主窗口 form1    按钮button1(打开form2)              按钮button2(退出程序)
子窗口 form2    按钮button1(打开form3)              按钮button2(返回form1)
子窗口 form3    按钮button1(关闭所有主或子窗口)     按钮button2(返回form1)
我在网上找的思路是:
form1:    
      button1: form2 f2 = new form2();             button2:  this.close();
               this.hide();                                  //this.dispose();
               f2.owner = this;
               f2.Showdialog();form2:
      button1: form3 f3 = new form3();             button2:  this.close();
               this.hide();                                  this.owner.show();
               f3.owner = f2.owner();
               f3.Showdialog();form3:
      button1: this.close();                       button2: this.close();              
               this.owner.close()                           this.owner.show();
              //this.owner.dispose();
问题如下:
1. 如果我用这个的方式定义按钮的话,可以通过按钮1,2在3个窗口间切换,可是一旦我要点关系到关闭整个程序的按钮(form1的button2和form3的button1)的时候,程序就卡死, 2. 我试着用dispose替换close 可是在button2的this.owner.show() 或 form3的button1 this.owner.dispose();会出现错误NullReferenceException(Object reference not set to an instance of an object.)3. 请问各位前辈,我的方法应该怎么改进才能"完美"的在3个或多个窗口之间通过2个按键逐层切换,我的方法也是在网上看到的, 或者也可以不用这个方法,你们有什么更好的方法也请指点,只要能达到效果就行。

解决方案 »

  1.   

    在每个窗体的formclosing中,整一个application.exit(),就行了。
      

  2.   


    感谢您的回复, 我刚刚实验了您的方法, 可是还是出现这个Exception
    Object reference not set to an instance of an object.
      

  3.   

     
                Form2 f2 = new Form2();
                f2.FormClosed += (s, j) => { this.Show(); };//关闭窗体2时显示当前窗体
                this.Hide();
                f2.ShowDialog();窗体2 button2点击事件  private void button2_Click(object sender, EventArgs e)
            {
                
                this.Close();
            }
      

  4.   

    1. 如果我用这个的方式定义按钮的话,可以通过按钮1,2在3个窗口间切换,可是一旦我要点关系到关闭整个程序的按钮(form1的button2和form3的button1)的时候,程序就卡死,
    --------------------
    简单的办法就是你把已打开窗体的 owner 设置为null 即可
      

  5.   

    详细代码如下:
    Form1
    1.button1 打开Form2 Form2 f2 = new Form2();
                f2.FormClosed += (s, j) => { this.Show(); };//关闭窗体2时显示当前窗体
                this.Hide();
                f2.ShowDialog();2.button2 关闭整个程序
    this.Close();Form2
    1.button1
      Form3 f3 = new Form3();
                f3.FormClosed += (s, j) => { this.Close(); };//窗体3关闭时关闭2,同时显示窗体Form1
                this.Hide();
                f3.ShowDialog();
    2.button2
    this.Close();
    Form3
    1.button1 关闭整个
    Application.Exit();
    2.button2
    this.Close();