wpf - Making instances of forms public C# -
i attempting pass information child form parent. have been using following code found on forum me:
form1.cs
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; namespace childform { public partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { form2 tempdialog = new form2(this); tempdialog.showdialog(); } public void msgme() { messagebox.show("parent function called"); } } }
form2.cs
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; namespace childform { public partial class form2 : form { private form1 m_parent; public form2(form1 frm1) { initializecomponent(); m_parent = frm1; } private void button1_click(object sender, eventargs e) { m_parent.msgme(); } } }
which works , , good. trouble is, program requires me set variables within tempdialog, form 1, in methods other button1_click. but, these cannot find instance of tempdialog because in button1_click.
also, cannot move out of method (say, class) because 'this' modifier not reference form1...
any ideas how can reference form1 form2 , vice versa? using code or otherwise?
thanks
i'm not sure mean last comment this
. make field references form2
, initialize in constructor. can reference _form2
in other methods of form1
.
public partial class form1 : form { private form2 _form2; public form1() { initializecomponent(); _form2 = new form2(this); } private void button1_click(object sender, eventargs e) { _form2.showdialog(); } }
this sort of thing can difficult follow, when complexity increases. better way might make form1
, form2
have references common object can both manipulate.
Comments
Post a Comment