主窗口 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个按键逐层切换,我的方法也是在网上看到的, 或者也可以不用这个方法,你们有什么更好的方法也请指点,只要能达到效果就行。
子窗口 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个按键逐层切换,我的方法也是在网上看到的, 或者也可以不用这个方法,你们有什么更好的方法也请指点,只要能达到效果就行。
解决方案 »
- 请教!!制作简单游览器时的问题!!在线等++88 高手们帮忙
- An unhandled exception of type 'System.BadImageFormatException' occurred in CsA
- C# 新建一个文件夹
- 请问大家开发教学软件,比如说少儿教学软件,一般用什么工具好啊?谢谢!!
- 在C#中如何判断双精度数值的小数位(奇数)递增呢?急
- 如何让IE或WebBrowser窗口内自动打开word文件?
- TriggerBox是什么控件?
- 如何使用 object.GetType().GetMethod() 用参数使用方法
- 请教大侠们,如何让主窗体等待一个子窗体运行结束再继续运行?
- form窗体
- 请教一个Timer控制picturebox显示imagelist图片的问题
- 如何对datagridview列进行排序,自动排序出错,如何自定义排序
感谢您的回复, 我刚刚实验了您的方法, 可是还是出现这个Exception
Object reference not set to an instance of an object.
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();
}
--------------------
简单的办法就是你把已打开窗体的 owner 设置为null 即可
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();