c# - Passing string between form using property doesnt work -
i know there's lot of posts here explaining how pass string between form using property method, can't make work. have form1 , form2, in form1, open bitmap, , want pass path form2, can access bitmap later. if change property value inside form1, it's ok, when load form2 , try access information, null. don't know doing wrong, if take , find mistake, apreciate.
here code
form1
public partial class form1 : form { public form1() { initializecomponent(); } string teste; public string filepath // property { { return teste; } set { teste = value; } } private void openmap_click(object sender, eventargs e) // opens bitmap { try { openfiledialog open = new openfiledialog(); if (open.showdialog() == dialogresult.ok) { bitmap bit = new bitmap(open.filename); picturebox2.image = bit; picturebox2.borderstyle = borderstyle.fixed3d; picturebox2.sizemode = pictureboxsizemode.stretchimage; this.filepath = open.filename; // update property } } catch (exception) { throw new applicationexception("falied loading image"); } } private void next1_click(object sender, eventargs e) // opens form2 { form2 inicio = new form2(); inicio.show(); this.hide(); } private void ssc1_1_load(object sender, eventargs e) { } }
}
form 2
public partial class form2 : form { public form2() { initializecomponent(); } private void form2_load(object sender, eventargs e) { } private void button1_click(object sender, eventargs e) { form1 obj = new form1(); messagebox.show(obj.filepath); } }
the problem messagebox null..
thanks in advance cheers
if want use property send information, form 2 needs reference instance of form 1 access it.
right creating new instance of form 1 form 2, not same instance 1 stored value in parameter, parameter of form doesn't have value.
you can pass referene form 1 constructor of form 2:
form2 inicio = new form2(this);
in constructor store reference later:'
private form1 _form1; public form2(form1 form1) { _form1 = form1; initializecomponent(); }
when need value in property, use reference:
messagebox.show(_form1.filepath);
an alternative using property , passing form reference constructor, pass value constructor. however, having reference form allows add more properties if need pass more information between forms.
Comments
Post a Comment