4

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)

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • Please check this link may be its help full for you [link](http://stackoverflow.com/questions/12225875/how-can-i-set-the-default-visibility-of-the-main-winform/12226758#12226758) – andy Sep 08 '12 at 11:05
  • 2
    possible duplicate of [C# Working with mutliple Forms](http://stackoverflow.com/questions/10769193/c-sharp-working-with-mutliple-forms) – Hans Passant Sep 08 '12 at 11:34

2 Answers2

8

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

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

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.

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. 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();
    }
    
  2. The application will not exit when you close the form. If you close all forms, the process will be still running, so you need to call Application.Exit(); when you want the application to exit (e.g. form closing event).

Tibi
  • 4,015
  • 8
  • 40
  • 64
0

Or, you just cannot understand that a modal window cannot live without a parent. When the parent is minimized, all the modal forms became hidden. When the parent is closed, the same is happening to the modal forms.

So it is a bad practice to trick the model form to stay alive after the parent dies. Just try to stay the pattern in which your second form is just another same equal form as the first one.

Ed_
  • 973
  • 11
  • 25
AgentFire
  • 8,944
  • 8
  • 43
  • 90