亲子父母而不亲子 [英] Closing Parent without closing child

查看:75
本文介绍了亲子父母而不亲子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中会弹出一个设置对话框(父母)。当用户单击继续时,将打开一个主对话框(子级)。
在主对话框中,用户可以重新编辑设置对话框(父级)。当用户单击X关闭安装对话框时,应用程序终止。
我认为这是因为我们关闭了父级,并且它处置了所有子级

I have a project where a setup dialog (Parent) pops. When the user hit continue a main dialog is opened (Child). From within the main dialog the user can re-edit the setup dialog (Parent). When the user clicks on X to close the setup dialog, the application terminates. I assume this is because we close the Parent and it disposes all its children

是否可以在不关闭主级的情况下关闭父级(或隐藏它)对话(孩子)?
如果不行,以下修复程序将起作用?
以父级打开主对话框,并打开设置对话框(子级)

Is it possible to close the Parent (or hide it) without closing the main dialog (child)? If not would the following fix would work? Open the Main Dialog as Parent and make it open the setup dialog (Child)

推荐答案

Program.cs 文件,您可能具有以下功能:

In the Program.cs file, you probably have a function like this:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

此功能是您应用程序的入口。 Application.Run()函数运行应用程序的主循环。图形应用程序的 main循环是事件处理程序触发事件,UI得到更新等的地方。如果事件(例如,按下按钮)花费的时间太长,则UI会挂起。为了防止这种情况的发生,可以使用线程。

This function is the entry point for your application. The Application.Run() function runs the main loop of the application. The main loop of a graphical application is the place where the event handler triggers the events, the UI gets updated and so on. If an event (for example pressing a button) takes too long to process, the UI hangs. To prevent this from happening, one can use threading.

Application.Run()函数已重载,因此如果函数具有参数(在这种情况下为 new Form1()),则该表单将成为主表单,因此在关闭该表单时主循环将退出。

The Application.Run() function is overloaded, so if the function has a parameter (new Form1() in this case), the form becomes the 'main' form, so the main loop will exit when the form is closed.

要解决此问题,您需要删除参数,该参数将使主循环在窗体关闭时不关闭就运行:

To fix this issue, you need to remove the parameter, which will make the main loop run without closing when the form closes:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run();
    }

但是,这会产生两个问题:

However, this creates 2 problems:


  1. 启动时不显示任何表单,因为我们从 Main 函数中删除了该表单。要解决此问题,您需要在main函数中创建一个新表单,并显示它:

  1. No form is displayed at start-up, because we removed that from the Main function. To fix this, you need to create a new form in the main function, and show it:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Form1 form = new Form1();
    form.Show();

    Application.Run();
}


  • 关闭表单时,应用程序不会退出。如果关闭所有表单,该过程仍将继续运行,因此要退出应用程序(例如,关闭表单事件)时,需要调用 Application.Exit();

    这篇关于亲子父母而不亲子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆