I have a form(MdiParent) with the following code:
public partial class Main : Form
{
Form1 frm1 = null;
Form2 frm2 = null;
}
With a couple of menus I do the following:
private void toolStripMenuItem_ClickForm2(object sender, EventArgs e)
{
if(frm2 == null)
{
frm2 = new Form2(frm1);
frm2.MdiParent = this;
}
frm2.Show();
}
private void toolStripMenuItem_ClickForm1(object sender, EventArgs e)
{
if(frm1 == null)
{
frm1 = new Form1();
frm1.MdiParent = this;
}
frm1.Show();
}
If I click menuitemForm2 do some operations(DB access and populate a DatagridView, frm1 is never initialized in frm2) in form2 and then click menuitemForm1 in order to show frm1. It is going to produce two windows of the same form1.
Does anyone have faced this strange behaviour before?
Addendum because of the comments...
frm2 does something like this with frm1:
Form1 frm ;
public Form2(Form1 form1)
{
frm = form1;
}
public void Optionalmethod()
{
if(frm == null)
{
frm = new Form1();
frm.MdiParent = this.MdiParent;
}
frm.Show();
}
Optionalmethod()
is not executed during the testing.
According to my logic form1 can be initialized in two cases:
1. Inside form2
2. In the Main form
For me does not matter where form1 is initialized, but it is important to have only one instance of it. Thats is the reason because I decided to pass the reference even if it is null