Ian
Ian

Reputation: 47

Closing Forms in Windows Form App?

I have two forms, FormA and FormB.

FormA has two buttons, one to open FormB and one to exit.
FormB has one button, to close FormB and reopen FormA.

My code goes like this:

public class FormA
{
    private void btnOpenformB_Click(System.Object sender, System.EventArgs e)
    {
        FormB B = new FormB();
        this.Hide();
        B.Show();
    }
    private void btnExit_Click(System.Object sender, System.EventArgs e)
    {
        this.Close();
    }
}

public class FormB
{
    private void btnReopenA_Click(System.Object sender, System.EventArgs e)
    {
        FormA A = new FormA();
        this.Close();
        A.Show();
    }
}

My problem is when I click the button on FormB to reopen FormA, and when I click the exit button on FormA, it doesn't stop debugging. What should I do? Thanks!

Upvotes: 1

Views: 828

Answers (5)

No Idea For Name
No Idea For Name

Reputation: 11577

lan, the problem with your code is that you don't return to the old formA when you press btnReopenA in formB, instead you open a new formA.

an elegant way to avoid that will be to register to the FormClosing event

private void btnOpenformB_Click(System.Object sender, System.EventArgs e)
{
    FormB B = new FormB();
    B.FormClosing += b_FormClosing;
    this.Hide();
    B.Show();
}

  void b_FormClosing(object sender, FormClosingEventArgs e)
  {
     Show();
  }

or, if you don't want to deal with events you can do this:

public partial class FormB : Form
{
  private Form _frm;
  public FormB(Form frm)
  {
     _frm = frm;
     InitializeComponent();
  }

  private void btnReopenA_Click(System.Object sender, System.EventArgs e) {
       if(_frm!=null) _frm.Show();
       this.Close();
  }
}

and when creating formB:

private void btnOpenformB_Click(System.Object sender, System.EventArgs e)
{
    FormB B = new FormB(this);
    this.Hide();
    B.Show();
}

Upvotes: 1

King King
King King

Reputation: 63317

public class FormA {
  private void btnOpenformB_Click(System.Object sender, System.EventArgs e) {
    FormB B = new FormB();
    this.Hide();
    B.Show(this);//Note we pass in the Owner here
  }
  private void btnExit_Click(System.Object sender, System.EventArgs e) {
    this.Close();
  }
}

public class FormB {
  private void btnReopenA_Click(System.Object sender, System.EventArgs e) {
    if(Owner!=null) Owner.Show();
    this.Close();
  }
}

Upvotes: 2

Bolu
Bolu

Reputation: 8786

My problem is when I click the button on FormB to reopen FormA, and when I click the exit button on FormA, it doesn't stop debugging.

This is because you opened another instance of FormA form your FromB

What should I do?

You need to get a reference of FormA in FormB and Show that instead.

How do i do it?

    public class FormA
    {
        private void btnOpenformB_Click(System.Object sender, System.EventArgs e)
        {
            FormB B = new FormB();
            B.Closed+=OnFromBClosed; //Add this to handle FromB Closed event
            this.Hide();
            B.Show();
        }
        private void btnExit_Click(System.Object sender, System.EventArgs e)
        {
            this.Close();
        }

       //Show FormA again when FromB is closed
        protected void OnFromBClosed(object sender, EventArgs e)
        {
            this.Show();
        }

    }

public class FormB
{
    private void btnReopenA_Click(System.Object sender, System.EventArgs e)
    {
       // FormA A = new FormA(); remove this.
        this.Close();
       // A.Show();  and remove this
    }
}

Upvotes: 0

ineteagle
ineteagle

Reputation: 21

Use Application.OpenForms[] collection: Application.OpenForms["FormA"].Show()

Upvotes: 0

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4907

Open second form in dialog mode:

this.Hide();
B.ShowDialog();
this.Show();

Upvotes: 1

Related Questions