如何从Windows窗体2.0中的子窗体关闭父窗体?

时间:2022-08-25 23:58:03

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应用程序中关闭子窗体内的父窗体。最好的方法是什么?

7 个解决方案

#1


3  

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中关闭表单时,它会处理它的所有子项。所以这不是一个好主意。您需要异步执行此操作,例如,您可以向父表单发送消息。

#2


4  

I ran across this blog entry that looks like it will work and it uses the Event Handler concept from D2VIANT Answer

我跑过这个博客条目,看起来它会工作,它使用D2VIANT答案中的事件处理程序概念

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();
    }

#3


0  

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类没有提供对'parent'表单的任何类型的引用,因此没有直接访问父类的方法(除非它恰好是MDI父类,在这种情况下你可以通过MDIParent属性访问它) )。您必须在子项的构造函数或属性中传递对父项的引用,然后记住设置它,然后使用该引用强制关闭父项。

#4


0  

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).

也许考虑让父母订阅孩子的事件,孩子可以在想要关闭父母时触发该事件。然后父母可以处理它自己的关闭(以及孩子的关闭)。

#5


0  

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的形式来解决这个问题。

#6


0  

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:

我同意davidg;您可以将父表单的引用添加到子表单的构造函数,然后根据需要关闭父表单:

private Form pForm;
public ChildForm(ref Form parentForm)
{
    pForm = parentForm;
}

private closeParent()
{
    if (this.pForm != null)
        this.pForm.Close();
    this.pForm = null;
}

#7


0  

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.

问题(可以肯定):启动应用程序(主窗体)>打开主窗体的子窗体VIA按钮或任何事件>关闭(主窗体)但子窗体也关闭。

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:

示例:尝试此操作以获取完整路径:

  1. string FullPath = Environment.CurrentDirectory + "\\YourAppName.exe";

    string FullPath = Environment.CurrentDirectory +“\\ YourAppName.exe”;

  2. Process.Start(FullPath);.

  3. this.Close();

    • this way you'll get to keep every form you want to keep open.
    • 这样你就可以保持你想要保持开放的每一个形式。

  4. this.Close();这样你就可以保持你想要保持开放的每一个形式。

#1


3  

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中关闭表单时,它会处理它的所有子项。所以这不是一个好主意。您需要异步执行此操作,例如,您可以向父表单发送消息。

#2


4  

I ran across this blog entry that looks like it will work and it uses the Event Handler concept from D2VIANT Answer

我跑过这个博客条目,看起来它会工作,它使用D2VIANT答案中的事件处理程序概念

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();
    }

#3


0  

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类没有提供对'parent'表单的任何类型的引用,因此没有直接访问父类的方法(除非它恰好是MDI父类,在这种情况下你可以通过MDIParent属性访问它) )。您必须在子项的构造函数或属性中传递对父项的引用,然后记住设置它,然后使用该引用强制关闭父项。

#4


0  

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).

也许考虑让父母订阅孩子的事件,孩子可以在想要关闭父母时触发该事件。然后父母可以处理它自己的关闭(以及孩子的关闭)。

#5


0  

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的形式来解决这个问题。

#6


0  

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:

我同意davidg;您可以将父表单的引用添加到子表单的构造函数,然后根据需要关闭父表单:

private Form pForm;
public ChildForm(ref Form parentForm)
{
    pForm = parentForm;
}

private closeParent()
{
    if (this.pForm != null)
        this.pForm.Close();
    this.pForm = null;
}

#7


0  

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.

问题(可以肯定):启动应用程序(主窗体)>打开主窗体的子窗体VIA按钮或任何事件>关闭(主窗体)但子窗体也关闭。

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:

示例:尝试此操作以获取完整路径:

  1. string FullPath = Environment.CurrentDirectory + "\\YourAppName.exe";

    string FullPath = Environment.CurrentDirectory +“\\ YourAppName.exe”;

  2. Process.Start(FullPath);.

  3. this.Close();

    • this way you'll get to keep every form you want to keep open.
    • 这样你就可以保持你想要保持开放的每一个形式。

  4. this.Close();这样你就可以保持你想要保持开放的每一个形式。