How to close the current form and then open the main one in C #?

I can get my first form called "FieldReprogrammingSoftware" to close, and open another "Setup" form using the following code.

Setup fh = new Setup(); fh.Show(); this.Hide(); 

However, I cannot do the same to return to FieldReprogrammingSoftware, because this name is not even an option in the installation code. If I try to simply enter FieldReprogrammingSoftware, I get an error message:

The name 'FieldReprogrammingSoftware' does not exist in the current context

Any ideas?

Please keep in mind that I am noob for C #.

NVM, I just found out that it’s normal for this product to keep the FieldReprogrammingSoftware form open while the installation form is inserted, so now I just use

  Setup fh = new Setup(); fh.Show(); 

in the form of FieldReprogrammingSoftware and

 this.close() 

in the form of installation. It was much easier for God to do in VB.

And the namespace for both forms is FieldUpdate, which is what my solution calls.

+4
source share
5 answers

In the installation form, you can use this:

 private void button1_Click(object sender, EventArgs e) { Application.OpenForms[0].Show(); this.Hide(); // or this.Close(); } 

this will display the first form again. "0" is the index of the main form of the application.

+4
source

You either need to pass an instance of the main form to Setup , or create a static variable to hold the main form.

You can also sign up for Setup Closed Form event, and then display the form again.

Thus

  Setup setupForm = new Setup(); setupForm.Show(); setupForm.FormClosed += (o,e) => this.Show(); this.Hide(); 

Edit for .NET 2.0

  Setup setupForm = new Setup(); setupForm.Show(); setupForm.FormClosed += new FormClosedEventHandler(setupForm_Closed); this.Hide(); void setupForm_Closed(object sender, FormClosedEventArgs e) { this.Show(); } 
+4
source

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.

+4
source

If you just need to use the installation form temporarily, another approach would be to open the form as a dialog.

 using(Setup fh = new Setup()) { this.Hide(); fh.ShowDialog(); this.Show(); } 
+1
source

Follow your heart, let the error message become your guide.

Check your form FieldProgrammingSoftware The namespace located at the top of the code file, right under your operations. Sometimes, especially when copying code or adding new forms from other projects, it is easy to mix namespaces.

For example, if you create a new project called WindowsApplication1 , all new forms that you add to the project will automatically inherit this namespace and will have WindowsApplication1 as their namespace. If you add a form from another project to an existing project, it will save the namespace from another project.

If you are not intentionally adding a new namespace to your code, which doesn't look like what you are doing, just make sure both matches (or both have at least a namespace). Otherwise, add a new namespace or click at the end of an object that you will not get intellisense for, and press Ctrl. to find out if VS can find the object namespace for you.

0
source

All Articles