[英]Owned form and mdi parent
this is my scenario and hope you can solve it for me 这是我的情景,希望你能为我解决
I have a MDI container form called "MainForm". 我有一个名为“MainForm”的MDI容器表单。 In MainForm there is a simple form call "Form1".
在MainForm中有一个简单的表单调用“Form1”。 In Form1 there is a button.
在Form1中有一个按钮。 every time you pushed it, It open a new form which instance of "Form2".
每次推动它时,它会打开一个新表单,其中包含“Form2”的实例。 The followng code is click button event.
以下代码是单击按钮事件。
Button_Click()
{
Form2 frm=new Form2();
frm.mdiparnt=this.MdiParent;
this.addOwnedForm(frm);
frm.Visible=true;
}
and the following code tries to close owned forms when the user close Form1 当用户关闭Form1时,以下代码尝试关闭所拥有的表单
Form1_CloseEvent()
{
foreach(var item in this.ownedForm)
{
item.close();
}
}
But when the debugger steps into close event, just close Form1, and the form2 instances remain open. 但是当调试器进入close事件时,只需关闭Form1,并且form2实例保持打开状态。 what should I do to solve it
我该怎么做才能解决它
I think you are not setting up the event. 我想你没有参加比赛。 Do it like this.
像这样做。
Add it to your Button_Click()
method: 将它添加到
Button_Click()
方法:
this.FormClosed += Form1_FormClosed;
Here is the method: 这是方法:
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
foreach(var item in this.ownedForm)
{
item.close();
}
}
first of all this code does not compile! 首先这个代码不编译!
you have several syntax errors: mdiparnt
, addOwnedForm
, ownedForm
, close
你有几个语法错误:
mdiparnt
, addOwnedForm
, ownedForm
, close
you are probably not sharing your actual code and that's gonna be a problem to help you if it's not your code. 你可能没有分享你的实际代码,如果这不是你的代码那将是一个帮助你的问题。
now in Button_Click()
event you are doing 现在在你正在做的
Button_Click()
事件中
frm.mdiparnt=this.MdiParent;
this.AddOwnedForm(frm);
even though you only need 即使你只需要
this.AddOwnedForm(frm);
or an exception will be thrown. 或者会抛出异常。 i've checked this code and it's working just fine
我检查了这段代码,它工作得很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.