2

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?

manlikeangus
  • 421
  • 9
  • 28
  • You are creating a new *mainForm* object, you are not making the hidden one visible. Surely not what you meant. Consider [this approach](http://stackoverflow.com/a/2342320/17034). – Hans Passant Oct 22 '14 at 06:30

2 Answers2

1

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();
        }
Ashok Rathod
  • 840
  • 9
  • 24
0

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.

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
Triple K
  • 379
  • 1
  • 3
  • 14