1

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!

1
  • 3
    With FormA A = new FormA(); you're creating a new form, when you do A.Show(); you're not showing FormA that you have hidden (this.Hide();) Commented Sep 3, 2013 at 9:48

5 Answers 5

2
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(); } } 
Sign up to request clarification or add additional context in comments.

Comments

1

Open second form in dialog mode:

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

Comments

1

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(); } 

Comments

0

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

Comments

0

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 } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.