C# 如何从 Windows Forms 2.0 中的子窗体关闭父窗体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I close a parent Form from child form in Windows Forms 2.0?
提问by Jason Von Ruden
I have a need to close a parent form from within child form from a Windows application. What would be the best way to do this?
我需要从 Windows 应用程序的子窗体中关闭父窗体。什么是最好的方法来做到这一点?
采纳答案by aku
When you close form in WinForms it disposes all of it's children. So it's not a good idea. You need to do it asynchronously, for example you can send a message to parent form.
当您在 WinForms 中关闭表单时,它会处理所有的子项。所以这不是一个好主意。您需要异步执行,例如您可以向父表单发送消息。
回答by dguaraglia
The Form class doesn't provide any kind of reference to the 'parent' Form, so there's no direct way to access the parent (unless it happens to be the MDI parent as well, in which case you could access it through the MDIParent property). You'd have to pass a reference to the parent in the constructor of the child, or a property and then remember to set it, and then use that reference to force the parent to close.
Form 类不提供对“父”表单的任何类型的引用,因此没有直接访问父级的方法(除非它恰好也是 MDI 父级,在这种情况下,您可以通过 MDIParent 属性访问它)。您必须在子的构造函数或属性中传递对父的引用,然后记住设置它,然后使用该引用强制关闭父。
回答by D2VIANT
Perhaps consider having the parent subscribe to an event on the child, and the child can fire that event whenever it wants to close the parent. The parent can then handle it's own closing (along with the child's).
也许考虑让父级订阅子级上的事件,子级可以在想要关闭父级时触发该事件。然后父母可以处理它自己的关闭(以及孩子的)。
回答by chrissie1
You are clearly noo using the correct way to open and close forms. If you use any form of MVC or MVP this problem would not arise.
您显然没有使用正确的方式打开和关闭表单。如果您使用任何形式的 MVC 或 MVP,则不会出现此问题。
So use a form of MVP or MVC to solve this problem.
所以使用MVP或者MVC的形式来解决这个问题。
回答by Ed Schwehm
I agree with davidg; you can add a reference to the parent form to the child form's constructor, and then close the parent form as you need:
我同意大卫;您可以将父窗体的引用添加到子窗体的构造函数中,然后根据需要关闭父窗体:
private Form pForm;
public ChildForm(ref Form parentForm)
{
pForm = parentForm;
}
private closeParent()
{
if (this.pForm != null)
this.pForm.Close();
this.pForm = null;
}
回答by Jason Von Ruden
I ran across this blog entry that looks like it will work and it uses the Event Handler concept from D2VIANT Answer
我遇到了这个看起来可以工作的博客条目,它使用了 D2VIANT Answer 中的事件处理程序概念
http://www.dotnetcurry.com/ShowArticle.aspx?ID=125
http://www.dotnetcurry.com/ShowArticle.aspx?ID=125
Summary: Step 1: Create a new Windows application. Open Visual Studio 2005 or 2008. Go to File > New > Project > Choose Visual Basic or Visual C# in the ‘Project Types' > Windows Application. Give the project a name and location > OK.
摘要: 第 1 步:创建一个新的 Windows 应用程序。打开 Visual Studio 2005 或 2008。转到“文件”>“新建”>“项目”>“项目类型”>“Windows 应用程序”中选择 Visual Basic 或 Visual C#。为项目命名和位置 > 确定。
Step 2: Add a n ew form to the project. Right click the project > Add > Windows Forms > Form2.cs > Add.
第 2 步:向项目添加一个新表单。右键单击项目 > 添加 > Windows 窗体 > Form2.cs > 添加。
Step 3: Now in the Form1, drag and drop a button ‘btnOpenForm' and double click it to generate an event handler. Write the following code in it. Also add the frm2_FormClosed event handler as shown below:
第 3 步:现在在 Form1 中,拖放一个按钮“btnOpenForm”并双击它以生成一个事件处理程序。在其中写入以下代码。还要添加 frm2_FormClosed 事件处理程序,如下所示:
private void btnOpenForm_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
frm2.Show();
this.Hide();
}
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
回答by Keyur Sureliya
There's a very easy solution to that.
对此有一个非常简单的解决方案。
Problem (To be sure) : Starts App(Main Form) > Open Child Form of Main Form VIA Button or Any Event > Closes (Main Form) But Child Form also Closes.
问题(可以肯定):启动应用程序(主窗体)> 通过按钮或任何事件打开主窗体的子窗体 > 关闭(主窗体)但子窗体也关闭。
Solution :
解决方案 :
Use :
Process.Start("Your_App's_EXE_Full_Path.exe");
用 :
Process.Start("Your_App's_EXE_Full_Path.exe");
Example : Try this to get full path:
示例:试试这个以获得完整路径:
string FullPath = Environment.CurrentDirectory + "\\YourAppName.exe";
Process.Start(FullPath);
.
this.Close();
- this way you'll get to keep every form you want to keep open.
string FullPath = Environment.CurrentDirectory + "\\YourAppName.exe";
Process.Start(FullPath);
.
this.Close();
- 通过这种方式,您将可以保持每个想要打开的表单。