manlikeangus
manlikeangus

Reputation: 421

How to Close Current Form and Return to Main Form

I have a couple of forms.

Let's call them mainForm, formA and formB.

On mainForm, there's a button that goes to formA and the button that does that has this bit of code:

private void buttonOpenFormA_Click(object sender, EventArgs e)
        {
            formA displayformA = new formA();
            displayformA.Show();

            this.Hide();
        }

And on formA, I have another button that opens formB like this:

private void buttonOpenFormB_Click(object sender, EventArgs e)
            {
                formB displayformB = new formB();
                displayformB.Show();

                this.Hide();
            }

And to return to mainForm:

private void buttonGoBack_Click(object sender, EventArgs e)
        {
            mainForm displayMainForm = new mainForm();
            displayMainForm.Show();

            this.Close();
        }

And on formA, this works beautifully. But on formB, however, this same block of code refuses to show the mainForm. What am I doing wrong?

Upvotes: 2

Views: 7791

Answers (2)

Triple K
Triple K

Reputation: 399

Main Form.cs

private void buttonOpenFormA_Click(object sender, EventArgs e){

            formA displayformA = new formA();

            displayformA.ShowDialog();

            //to close the form
            this.dialogResult=DialogResult.OK;
}

FormA.cs

    private void buttonOpenFormB_Click(object sender, EventArgs e){

                    formB displayformB = new formB();

                    displayformB.ShowDialog();

                    //to close the form
                    this.dialogResult=DialogResult.OK;
    }

private void buttonGoBack_Click(object sender, EventArgs e){

                //to close the form

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
}

You only need to use ShowDialog(). Whenever you want to close,use this.DialogResult=DialogResult.OK.

Upvotes: 0

Ashok Rathod
Ashok Rathod

Reputation: 840

I think you can simple pass your MainForm Object to FormA then FormA to FormB then on click of button you should simple show your FormA object.

As per your code, you are here showing new MainForm object which is not you have created first time as you are creating new object in buttonGoBack_Click event.

You should need to make change in FormA

public MainForm mainForm {get;set;}

public  FormA(MainForm mainForm)
{
  this.mainForm= mainForm;

}

You should need to make change in FormB

public MainForm mainForm {get;set;}

public  FormB(MainForm mainForm)
{
  this.mainForm= mainForm;

}


        private void buttonOpenFormA_Click(object sender, EventArgs e)
        {
            formA displayformA = new formA(this);
            displayformA.Show();

            this.Hide();
        }

             private void buttonOpenFormB_Click(object sender, EventArgs e)
            {
                formB displayformB = new formB(this.mainForm);
                displayformB.Show();

                this.Hide();
            }

        private void buttonGoBack_Click(object sender, EventArgs e)
        {
            (this.mainform as MainForm).Show();
             this.Close();
        }

Upvotes: 1

Related Questions