Share a object between Main and Child window as a means of passing data back and forth (C#) -
this problem has 3 files:
options.cs
(a datastore option values)mainwindow.cs
(the main window program, appearnce modified based on options inside options datastore - once class decalred object)optionswindow.cs
(the child window user sets option value, window saves changesoptions
datastore when user presses button inside window)
what want share data between main window form , child window form in c#. main window called mainwindow
, child window optionswindow
.
i have data store called options
. object stores various user options. (some type int
, bool
) these options used mainwindow
appearance purposes.
so user makes changes in optionswindow
, these variables changes saved in useroptions
object of type options
declared inside optionswindow
.
i did because need easy , simple way move set data in childwindow, save data object , use data in mainwindow
why did not declare useroptions
object inside mainwindow
? because can't access data declared in mainwindow
unless make object public
says never ever do. have tried making useroptions
object inside optionswindow
, exposing varaibles (data) through properties.
this works. can access , set properties both optionswindow
, mainwindow
.
whats problem? optionswindow.show();
can used once, after c# not let ever work again, reason once use only. seems object useroptions
being destroyed once form closes.
mainwindow.cs
public partial class mainwindow : form { optionswindow optionswindow = new optionswindow();
optionswindow
declares object useroptions
of type options
. datastore. rest of optionswindow
getting user data , adding data options datastore
public partial class optionswindow : form { options useroptions = new options(); public bool allowtooltip { { return useroptions.allowtooltips; } set { useroptions.allowtooltips = value; } }
i call optionswindow
this: (note works once because of exceptions errors, apparently c# can't reuse child objects. confusing me)
private void optionstoolstripmenuitem1_click(object sender, eventargs e) { try { optionswindow.show();// stops working after first call } catch { }; }
i have been working on code 4 hours trying everything.
all want have data store of user options of various types , use options window set options , main window read options.
that want do.
set variables shared datastore inside child window , access datastore in main window.
the data store object.
is fixable? or there better way data child-window can accessed both child-window , main-window.
edit: ended making datastore public because code becoming convoluted.
try adding options property optionswindow form.
public partial class optionswindow : form { public options { get; set; } public bool allowtooltip { { return options.allowtooltips; } set { options.allowtooltips = value; } } }
in mainwindow set options , read when done.
private void optionstoolstripmenuitem1_click(object sender, eventargs e) { optionswindow.options = myoptions; optionswindow.showdialog(); myoptions = optionswindow.options; }
Comments
Post a Comment