المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Pass data between two child forms



C# Programming
07-23-2009, 09:11 PM
Hi guys!
Please help, I am trying to pass data from form1 to form2 using a datagridview cell click event (passing data from datagridview1 in form1 to datagridview2 in form2)

It is working fine if I open form1 and then form2, if I open form2 before form1 then it doesn't because it can't capture instance of form1 to access it.

Here is what I am doing presently:
in Parent (MDI), I have a public form instance for form1, I use that when I open form1 and then pass that instance to form2 when I open it.

This is what I am doing:

In Parent:
public frmGrid1 frm1;
....
//Form1 click event in parent
frm1 = new frmGrid1(m_ADBCnn, 1, notWorkedToolStripMenuItem.Text);
frm1.MdiParent = this;
frm1.Show();
....
//Form2 click event in parent
frm2 = new frmGrid2(frm1,m_ADBCnn,1,presentToolStripMenuItem.Text);
frm2.Show();
frm2.MdiParent = this;


I then assign frm1 in the constructor of form2 as follows:

private frmGrid1 frm1;
public frmGrid2(frmGrid1 f1)
{
try
{
InitializeComponent();
frm1 = f1;
}
catch (Exception ex)
{
MessageBox.Show("Failed on initialization: " + ex.Message);
}
}

Again, this works if I open form1 before form2. How can I retrieve frm1 from frm2 if it is null?


Please help

Sameer