In the described situation, you want to return the original instance. You used this.hide to hide the form.
Obviously, you do not want to create a new instance because it is wasting resources; you need to save the original instance somewhere where you can get it from the child form.
There are several ways to do this, but I would suggest one of two ways:
Singleton:
Save your main form in singleton mode so that when you need to return it, you can refer to the saved instance, and not to the new one wastefully.
public static class GlobalForms { private MainForm _main; public MainForm Main { get { if (_main == null) _main = new MainForm(); return _mainForm; } } }
Now that you want MainForm, instead of updating it, you should reference singleton.
Form mainInstance = GlobalForms.Main mainInstance.Show(); //Do any other stuff for your main form. this.Hide(); //Or this.Close();
This loads lazily, so it wonβt create an instance of the main form until you need it the first time. After that, it will be held until you need it again, if you do not get rid of it, in which case the cycle will start again.
Opening the second form as a child of the first:
Form otherForm = new OtherForm(); otherForm.Show(this); //This sets up the main form as the owner of this one for this call this.Hide();
Alternatively, if you want OtherForm to open as a modal dialog (that is, you cannot interact with other windows while it is open), then run otherForm.ShowDialog(this);
And from OtherForm:
this.Owner.Show(); this.Close();
Alternatively, Stan R discussed a neat idea using an event handler to catch a child form closing event . Although this can potentially become difficult if you have several forms that open your child form, therefore, if this form is a common dialogue for many forms, this approach should be used with caution.
You can also access the Application.OpenForms collection to get the forms that are currently open in your application. You will need to know the index of the form you want to activate.
Each approach has its drawbacks. Naturally, the route you take will complement the rest of your architecture and design so that your code is supported.