日期:2014-05-18  浏览次数:21219 次

C# 多层子窗口 及 主窗口见的切换
主窗口 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个按键逐层切换,我的方法也是在网上看到的, 或者也可以不用这个方法,你们有什么更好的方法也请指点,只要能达到效果就行。

------解决方案--------------------
在每个窗体的formclosing中,整一个application.exit(),就行了。
------解决方案--------------------
C# code
 
            Form2 f2 = new Form2();
            f2.FormClosed += (s, j) => { this.Show(); };//关闭窗体2时显示当前窗体
            this.Hide();
            f2.ShowDialog();

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

 Form2 f2 = new Form2();
            f2.FormClosed += (s, j) => { this.Show(); };//关闭窗体2时显示当前窗体
            this.Hide();
            f2.ShowDialog();

------解决方案--------------------
探讨

详细代码如下:
Form1
1.button1 打开Form2
C# code

Form2 f2 = new Form2();
f2.FormClosed += (s, j) => { this.Show(); };//关闭窗体2时显示当前窗体
this.Hide();
f2.ShowDialog();


2……