c# - Form2 Value returns 0 when exit -
i have main form , form 2 (which modal form).
when enter form 2, there combo box , selected value stored in class. there, works fine because messagebox confirms value stored.
but when exit form 2 , main form display value in textbox, value returns 0.
form 2:
private void btnok_bs__spec_click(object sender, eventargs e) { bsit bsit = new bsit(); string spec = cboit_spec.text; { if (spec == "animation , game development" || spec == "digital arts") { bsit.setspec(spec); messagebox.show("you chose " + bsit.getspec() + ".", "specialization", messageboxbuttons.ok, messageboxicon.information); } else { messagebox.show("please select specialization."); } } while (bsit.getspec() == ""); }
class
public class bsit : student { public bsit() { spec = ""; } private string spec; public void setspec(string spec) { if (spec == "animation , game development" || spec == "digital arts") { this.spec = spec; } } public string getspec() { return spec; } }
main form (display value of spec)
private void txbxspec_input_textchanged(object sender, eventargs e) { bsit bsit = new bsit(); if (!(bsit.getspec() == "")) { txbxspec_input.text = bsit.getspec(); } }
you've got 2 separate instances of bsit
class. need pass first instance instance of form 2 or make bsit class static.
at least think that's cause, can see in code posted. don't see you're instantiating form contains btnok_bs__spec_click
event.
you're "new"ing instance of bsit
in btnok_bs__spec_click
event , saving value it, goes out of scope event ends lose value. you're trying user's value first instance of bsit
.
Comments
Post a Comment