Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close a current form and then open the main one in C#?

Tags:

c#

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

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

However I can't seem to do the same type of thing to get back to FieldReprogrammingSoftware because that name isn't even an option in the Setup code. If I try to just type FieldReprogrammingSoftware I get an error saying:

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

Any Ideas?

Please keep in mind that I'm a noob to C#.

NVM, I just found out that for this product it is ok to have the FieldReprogrammingSoftware form stay open while the Setup form is up so I'm now just using

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

on the FieldReprogrammingSoftware form and

this.close()

on the setup form. God this was alot easier to do in VB

And the namespace for both forms is FieldUpdate, which is what my Solution is called.

like image 484
Bryan Avatar asked Dec 13 '22 00:12

Bryan


2 Answers

In the Setup form you can use this:

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

this will show the first form again. "0" is the index of the application's main form.

like image 114
Inisheer Avatar answered Dec 28 '22 22:12

Inisheer


You either have to pass in the instance of the main form to Setup or create a static variable to hold the main form in.

You can also subscribe to Setup Form Closed event and then show the form again.

Like so

        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();
        }
like image 30
Stan R. Avatar answered Dec 29 '22 00:12

Stan R.