0

I have some code meant to open a new windows form when one is closed, and yet I get nothing, no error.

I've tried a few different methods for opening a new form on a Form.FormClosed event.

This is the code I have right now:

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Form1 myForm = new Form1();
    myForm.Show();
}

But yet I get no error, nothing.

I'm expecting for a new windows form to be opened when I close another one. Any help would be appreciated, thanks!

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
nijwons
  • 295
  • 1
  • 3
  • 8
  • Show the whole class that `Form1_FormClosed` is in... Have you set breakpoints? This could create an infinite loop... – Trevor Apr 25 '19 at 20:37
  • Do you want to open a new form, or prevent the user from closing the existing form? You can achieve the later by handling the `FormClosing` event. – Theodor Zoulias Apr 25 '19 at 20:39
  • 3
    how do you know that the form is not showing? looks like you Show it again immediately when FormClosed is fired. – Vasya Apr 25 '19 at 20:39
  • @TheodorZoulias no, i want to open a new form, not prevent the user from closing it. – nijwons Apr 25 '19 at 20:42
  • @Vasya How should I change it to check for that? – nijwons Apr 25 '19 at 20:45
  • 1
    Then look here: [How I can close a 1st form without closing the full application?](https://stackoverflow.com/questions/9639062/how-i-can-close-a-1st-form-without-closing-the-full-application) Basically you must remove the form as an argument of the `Application.Run()` command inside the `Main` method. – Theodor Zoulias Apr 25 '19 at 20:48
  • @nijwons: create a different form (not Form1) and see if it shows. You can also show Form1 at different position by changing Location property. – Vasya Apr 25 '19 at 21:03
  • Hi nijwons, if answers below were helpful please upvote. If either answer solved your problem, please choose the best answer and mark it as an answer. This helps those who have given time to help out and helps others searching for the same information in future. Thanks – pcdev Aug 08 '19 at 06:43

2 Answers2

0

The problem is that as soon as the first Form1 instance closes, your application shuts down and exits because the application message loop is defined with the initial Form instance, and it is just waiting for events on that form until it closes. On closing, the application will exit, opening a new form doesn't stop this process.

You need to adjust the Main() method in Program.cs to look something like this:

[STAThread]
static void Main()
{
    // ... Application configuration as required here

    new Form1().Show(); // The first form instance is now no longer bound to the Application message loop.  Start it before we begin the run loop
    Application.Run(); // Don't pass in Form1
}

Your original code should now work. I might add however, this is not a great user experience. Carefully consider what you're trying to achieve, and perhaps consider alternatives - do you just need a "reset form" button? Or is the primary goal to prevent a user from closing the application? If the latter, you can remove the Close icon altogether.

pcdev
  • 2,852
  • 2
  • 23
  • 39
  • I should also add as a postscript that if you decide to go ahead with this and you want a clean way to exit the application (like a "quit" button on your form) then you will need to add some sort of flag to indicate that the app should be closing, set this flag when the button is clicked and then call `Application.Exit()`. In your `Form1_FormClosed` method, check the flag and don't instantiate a new form if the flag is set. – pcdev Apr 25 '19 at 22:18
0

Perhaps something simple to get your going forward.

private Form1 myForm = new Form1(); //Declare the form as a private member variable

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    e.Cancel = true;       //Cancel the closing so this object stays alive
    this.Visible = false;  //Hide this form
    myForm.Show();         //Show the next form
}

Please note @pcdevs comment. You'll need a way to indicate the form is being closed/application quiting vs progressing to the next step/form. You might want to look at some CodeProject articles about "C# Winform Wizards", those sequential dialog prompt apps...

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321