C# – How to make two forms reference each other

Forms2’s code should 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;
    }
}
}

Even though the both talk to each other, one must be created first and passed to the second one.

Form1 will need to be tweeked to

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

The other way to do it is create both and pass it after construction

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

Leave a Comment