C# - How to Make Two Forms Reference Each Other

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

Communicate between two windows forms in C#

Form1 triggers Form2 to open. Form2 has overloaded constructor which takes calling form as argument and provides its reference to Form2 members. This solves the communication problem. For example I've exposed Label Property as public in Form1 which is modified in Form2.

With this approach you can do communication in different ways.

Download Link for Sample Project

//Your Form1

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}

public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
}

//Your Form2

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
this.mainForm.LabelText = txtMessage.Text;
}
}

alt text
(source: ruchitsurati.net)

alt text
(source: ruchitsurati.net)

Linking forms to each other by buttons makes multiple windows of same form

Move the creation of the opposing form outside of the click event.

frmInformation information = new frmInformation();

private void cmdInformation_Click(object sender, EventArgs e)
{
information.Show();
}

This will re-display the same form over and over maintaining any data even when the user closes it.

Access two C# projects from each other

Since A -> B and B -> A Creates circular dependency, what I did was I added all the components to one project by copying and changing the namespace, then I created separate folders for each and every module. No references needed. Seems to work fine.

How to switch between forms without creating new instance of forms?

Since you are accessing your forms sequentially just make sure that you use the Show Method that assigns the owner to the created Form and assign it to a class level variable after you create it. Something like this should work for you.

Form1

public partial class Form1 : Form
{
Form2 frm2;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (frm2 == null)
{
frm2 = new Form2(); //Create form if not created
frm2.FormClosed += frm2_FormClosed; //Add eventhandler to cleanup after form closes
}

frm2.Show(this); //Show Form assigning this form as the forms owner
Hide();
}

void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
frm2 = null; //If form is closed make sure reference is set to null
Show();
}
}

Form2

public partial class Form2 : Form
{
Form3 frm3;
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Owner.Show(); //Show the previous form
Hide();

}

private void button2_Click(object sender, EventArgs e)
{
if (frm3 == null)
{
frm3 = new Form3();
frm3.FormClosed += frm3_FormClosed;
}

frm3.Show(this);
Hide();
}

void frm3_FormClosed(object sender, FormClosedEventArgs e)
{
frm3 = null;
Show();
}
}

Form3

public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Owner.Show();
Hide();
}
}

Open two forms at the same time

You could try something like this:

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
Form3 frm3 = null;

frm2.Shown += (s, args) =>
{
frm3 = new Form3();
frm3.Show();
};

frm2.FormClosing += (s, args) =>
{
frm3.Close();
};

frm2.ShowDialog(this);
}

Form2 is shown using ShowDialog which will prevent Form1 from getting selected. When Form2 is shown, it shows Form3 in its Shown event. If Form2 is closed, it will also close Form3.



Related Topics



Leave a reply



Submit