Pass data between two child forms
-
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
-
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
Best way to do this would be with events[^], each form will have an event that fires when the cell is clicked (or whatever) the parent form then listens for this event. When it is fired then the parent form can receive the data from one form (through event args or property) and then pass the data to the other form (through method or property)
Life goes very fast. Tomorrow, today is already yesterday.
-
Best way to do this would be with events[^], each form will have an event that fires when the cell is clicked (or whatever) the parent form then listens for this event. When it is fired then the parent form can receive the data from one form (through event args or property) and then pass the data to the other form (through method or property)
Life goes very fast. Tomorrow, today is already yesterday.
-
Best way to do this would be with events[^], each form will have an event that fires when the cell is clicked (or whatever) the parent form then listens for this event. When it is fired then the parent form can receive the data from one form (through event args or property) and then pass the data to the other form (through method or property)
Life goes very fast. Tomorrow, today is already yesterday.
-
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
Here you are: Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^]
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion