c# - Keeping values of secondary Form -
i have 2 forms: mainform , optionsform, wich has button (ok) applies changes on mainform. when open optionsform first time ok, default values.
after make changes , and click ok options applied when open optionsform second time, wanted hold previous values, not default ones happening.
optionsform opened through mainform this
optionsform formoptions = new optionsform(); if (formoptions.showdialog(this) == dialogresult.ok) { // etc.. } //... public string otherlabel { { return formmainlabel.text; } set { formmainlabel.text = value; } } in optionsform have numericupdown , want hold value
private mainform mainform = null; public optionsform(form callingform) { mainform = callingform mainform; initializecomponent(); } // ... private void btnok_click(object sender, eventargs e) { this.mainform.somelabel= somebox.value.tostring(); // numericupdown this.mainform.otherlabel = "abc"; //>>> getting nullreferenceexception this.close(); } now can hold settings i'm getting nullreferenceexception. tryed this it's still not working. sugestion?
showdialog() made support this. different show(), other being modal, prevents form object being disposed when user closes it. can call showdialog() again, controls keep original values:
private optionsform options = new optionsform(); private void button1_click(object sender, eventargs e) { if (options.showdialog(this) == dialogresult.ok) { // etc.. } } protected override void onformclosed(formclosedeventargs e) { options.dispose(); base.onformclosed(e); }
Comments
Post a Comment