-1

I have a form1 that opens a form2 that opens a form3. I want to return to form1 from form3 using a button.

form1

private void form2button_Click(object sender, EventArgs e)
{
    this.Hide();
    form2 f2 = new form2();
    f2.ShowDialog();
    this.Show();
}

form2

private void form3button_Click(object sender, EventArgs e)
{
    this.Close();
    form3 f3 = new form3();
    f3.ShowDialog();
}
private void exitbutton_Click(object sender, EventArgs e)
{
    this.Close();
}

form3

private void mainmenubutton_Click(object sender, EventArgs e)
{
    this.Close();
}
private void backbutton_Click(object sender, EventArgs e)
{
    this.Close();
    form2 f2 = new form2();
    f2.ShowDialog();
}

But when in form3, after clicking the back button it shows form2 but form3 is still in the background. I have fixed this by adding this.Hide(); before this.Close();

I would like to know the logic behind why this happening.

Potato
  • 13
  • 6
  • Well, no, the form3button's Click event surely should not create a new Form2 instance and the backbutton must not create a new form object when the original one is still hidden. Consider to get rid of the Hide() hack with [this solution](https://stackoverflow.com/a/10769349/17034). A well-design GUI today ought to have only one main window, you can switch its content by swapping UserControls. – Hans Passant Oct 09 '18 at 09:31
  • The lesser drastic solution is to subscribe form2's FormClosing event and call this.Show() to make the form1 visible again. – Hans Passant Oct 09 '18 at 09:37
  • Another way would be to pass the instance of the form you need to return to, to the form you create. Also drop the `Hide` hack – GuidoG Oct 09 '18 at 10:51
  • @HansPassant the form3button creates Form3 and the backbutton creates Form2 again as it was closed when Form3 is created (there was a typo, my bad). Thanks for the solution, I didn't want to use it because I thought it was a very drastic one. – Potato Oct 09 '18 at 14:23

2 Answers2

0

Is there a way to have it appear in the same position?

form2 f1 = new form2();
f1.Location = Location; //location of any form, it also clould be f3.Location if it is exists
f1.ShowDialog();

Also is there a difference between this.Hide(); and Hide(); ?

No, there isn't.

Potato
  • 13
  • 6
koviroli
  • 1,422
  • 1
  • 15
  • 27
0

If I understand you right then this is what you need to in order to go back to the first form:

form1:

private void form2button_Click(object sender, EventArgs e)
{
    this.Hide();
    Account form2 = new Account();
    form2.ShowDialog();
    this.Show();
}

form2:

    private void form3button_Click(object sender, EventArgs e)
    {
        this.Hide();
        form2 f2 = new form2();
        f2.ShowDialog();
        this.Close();

    }
    private void exitbutton_Click(object sender, EventArgs e)
    {
        this.Close();
    }

form3:

private void mainmenubutton_Click(object sender, EventArgs e)
{
    this.Close();
}
private void backbutton_Click(object sender, EventArgs e)
{
    this.Close();
}

Also is there a difference between this.Hide(); and Hide(); ?

No, there's no any difference