C # - How to make two forms of link to each other

I am writing a WindowsForms application in MS Visual C # and I need two forms in order to be able to link to each other. When I test, I created two buttons on Form1 - one button that displays Form2 and another button that hides it (code below).

I want to do the same for Form2 - create two buttons that hide or show Form1. I used the same method as for Form1, but when I compile the application, it seems to fall into an infinite loop and I get a StackOverflow message.

How can I change the code so that both forms can refer to each other?

Form Code 1:

namespace WindowsFormsApplication1 { public partial class Form1 : Form { Form2 frm2; public Form1() { InitializeComponent(); frm2 = new Form2(); } private void button1_Click(object sender, EventArgs e) { frm2.Visible = false; } private void button2_Click(object sender, EventArgs e) { frm2.Visible = true; } } } 

Form Code 2:

 namespace WindowsFormsApplication1 { public partial class Form2 : Form { Form1 frm1; public Form2() { InitializeComponent(); frm1 = new Form1(); } private void button1_Click(object sender, EventArgs e) { frm1.Visible = false; } private void button2_Click(object sender, EventArgs e) { frm1.Visible = true; } } } 
+4
source share
6 answers

The form code must be

 namespace WindowsFormsApplication1 { public partial class Form2 : Form { Form1 frm1; public Form2(Form1 parent) { InitializeComponent(); frm1 = parent; } private void button1_Click(object sender, EventArgs e) { frm1.Visible = false; } private void button2_Click(object sender, EventArgs e) { frm1.Visible = true; } } } 

Despite the fact that both speak to each other, you must first create and transfer the second.

Form 1 must be tweeked for

 public Form1() { InitializeComponent(); frm2 = new Form2(this); } 

Another way to do this is to create both and pass it after building

 namespace WindowsFormsApplication1 { public class SomewhereElse { public void SomeFunction() { Form1 form1= new Form1(); Form2 form2= new Form2(); form1.frm2 = form2; form2.frm1 = form1; } } public partial class Form2 : Form { public Form1 frm1 {get; set;} public Form2(Form1 parent) { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { frm1.Visible = false; } private void button2_Click(object sender, EventArgs e) { frm1.Visible = true; } } public partial class Form1 : Form { public Form2 frm2 {get; set;} public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { frm2.Visible = false; } private void button2_Click(object sender, EventArgs e) { frm2.Visible = true; } } } 
+5
source

β€œOther form” must be provided to the appropriate copy. Something like this should help.

 public partial class TogglingForm : Form { TogglingForm Other {get; set;} public TogglingForm() { InitializeComponent(); } private void HideOther_Click(object sender, EventArgs e) { Other.Visible = false; } private void ShowOther_Click(object sender, EventArgs e) { Other.Visible = true; } } .... static void Main() { var first = new TogglingForm(); var second = new TogglingForm {Other = first}; first.Other = second; first.Show(); } 
+1
source

You create a new instance of each form when you create another form.

Instead, you should make one of the forms in order to take an instance of your parent form as a parameter.

0
source

When you create an instance of Form2 in the frm1 object, this new instance creates another instance of Form1 that creates a new instance of Form 2, which ...

Do you see an endless loop?

Create one instance of each other in the main class or as global application variables. Or pass them as a parameter to each other .. The .NET framework simply refers to them without allocating a new memory space.

0
source
 namespace WindowsFormsApplication1 { public partial class Form2 : Form { Form1 frm1; public Form2(Form1 frm1) { InitializeComponent(); this.frm1 = frm1; } private void button1_Click(object sender, EventArgs e) { frm1.Visible = false; } private void button2_Click(object sender, EventArgs e) { frm1.Visible = true; } } } 
0
source

All Articles