c# - how to pass data from a form to another form that is previously instantiated from any other form? -
i have 3 forms(form1,form2,form3), main form form1, opened form2 taking data , have update button on form2 take me form3, want whatever user writes on form3 updated form2, how can make possible using c#.net?
(i opened form2,form3 using showdialog() method)
//reference form2 form2 secondaryform = new form2(mainform);<br/> secondaryform.showdialog(); //in constructor of form2 save reference of form1 form1 form1 = null form2(form1 mainform) { form1 = mainform; } //then instead of creating new mainform again use reference of form1 form1.updatetext(data); this.close() i have used above code getting nullreference exception on form1.updatetext(data);
just pass form 2 reference form 3 while instantiating it.. did form1 while opening form2. form3 use form2 reference call updatetext method should public method on form2
here code 3 forms, can update form others, have made can access form1 , form2 in form3.
using system; using system.linq; using system.text; using system.windows.forms; namespace windowsformsapplication1 { public partial class form1 : form { public form2 frm2; public form3 frm3; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { } public void updatetext() { this.textbox1.text = ""; } private void button1_click(object sender, eventargs e) { if (frm2 == null) frm2 = new form2(this); frm2.showdialog(); } } } using system; using system.text; using system.windows.forms; namespace windowsformsapplication1 { public partial class form2 : form { public form1 reftoform1; public form2(form1 f1) { reftoform1 = f1; initializecomponent(); } private void button1_click(object sender, eventargs e) { if (reftoform1.frm3 == null) reftoform1.frm3 = new form3(this); reftoform1.frm3.showdialog(); } public void updateform2(string txt) { this.textbox1.text = txt; } } } using system; using system.linq; using system.text; using system.windows.forms; namespace windowsformsapplication1 { public partial class form3 : form { form2 reftoform2; public form3( form2 f2) { reftoform2 = f2; initializecomponent(); } private void button1_click(object sender, eventargs e) { //pass data form1; reftoform2.reftoform1.updatetext(); //pass data form2 reftoform2.updateform2("from form3"); } } }
Comments
Post a Comment