c# - Saving changes made to controls on form? -


question

how can save "settings" can used again after application has been closed?

when settings mean different properties of controls on form.

what easiest , appropriate method? i've read can save system registry or xml file?

background

i have form creates row in table changes depending on control set or change.

i able save different configurations , display them in combobox repeated use.

example

  1. the end user fill in textboxs , ticks checkboxes.
  2. they click add favourites
  3. they add favourite name , save
  4. the save permanently visible in favourites combobox.

my form

enter image description here


one way save data write registry, under hkcu node. way different users of application have own settings if app on same machine. keeps file system little cleaner , doesn't require database. downside favorites live on machine, , don't roam user across devices.

a way implement wrap form settings in class knows how save , load values registry. this, along registry helper class, make pretty easy add "favorites" functionality form.

for example, first create registry helper class read , write settings hkcu node (so settings specific logged in user):

public class reghelper {     private static readonly registrykey root = registry.currentuser         .createsubkey(@"software\companyname\applicationname");      private readonly registrykey _thiskey = root;      public reghelper() { }      public reghelper(string favoritekey)     {         _thiskey = root.createsubkey(favoritekey);     }      public list<string> getsubkeys()     {         return _thiskey.getsubkeynames().tolist();     }      public void setproperty(string propertyname, string value)     {         _thiskey.setvalue(propertyname, value, registryvaluekind.string);     }      public void setproperty(string propertyname, bool value)     {         setproperty(propertyname, value.tostring());     }      public string getproperty(string propertyname)     {         return getproperty(propertyname, string.empty);     }      public string getproperty(string propertyname, string defaultvalue)     {         return _thiskey.getvalue(propertyname, defaultvalue).tostring();     }      public bool getpropertyasbool(string propertyname)     {         return bool.parse(getproperty(propertyname, default(bool).tostring()));     } } 

then, wrap fields of form class not has properties match form fields, has methods save values registry , static methods load favorites or specific named favorite. example:

public class favorite {     public string name { get; private set; }     public string notes { get; set; }     public bool notesfromplanner { get; set; }     public string project { get; set; }     public string dblocation { get; set; }     public string assesmenttoolversion { get; set; }     public string projectcodes { get; set; }     public bool straighttonew { get; set; }      public favorite(string name)     {         this.name = name;     }      public void save()     {         var reg = new reghelper(this.name);         reg.setproperty("name", name);         reg.setproperty("notes", notes);         reg.setproperty("notesfromplanner", notesfromplanner);         reg.setproperty("project", project);         reg.setproperty("dblocation", dblocation);         reg.setproperty("assesmenttoolversion", assesmenttoolversion);         reg.setproperty("projectcodes", projectcodes);         reg.setproperty("straighttonew", straighttonew);     }      public static favorite getfavorite(string favoritename)     {         var reg = new reghelper(favoritename);         return new favorite(favoritename)         {             notes = reg.getproperty("notes"),             notesfromplanner = reg.getpropertyasbool("notesfromplanner"),             project = reg.getproperty("project"),             dblocation = reg.getproperty("dblocation"),             assesmenttoolversion = reg.getproperty("assesmenttoolversion"),             projectcodes = reg.getproperty("projectcodes"),             straighttonew = reg.getpropertyasbool("straighttonew"),         };     }      public static list<favorite> getfavorites()     {         return new reghelper().getsubkeys().select(getfavorite).tolist();     }      public override string tostring()     {         return this.name;     } } 

then, use favorite class populate favorites drop down:

private void form1_load(object sender, eventargs e) {     // saved favorites , load them in combo box     foreach (var favorite in favorite.getfavorites())     {         cbofavorites.items.add(favorite);     } } 

now, when favorite picked combo box, want populate our form details:

private void cbofavorites_selectedindexchanged(object sender, eventargs e) {     var favorite = (favorite) cbofavorites.selecteditem;      txtnotes.text = favorite.notes;     txtassettoolversion.text = favorite.assesmenttoolversion;     txtdblocation.text = favorite.dblocation;     chknotesfromplanner.checked = favorite.notesfromplanner;     txtprojectcodes.text = favorite.projectcodes;     cboprojects.text = favorite.project;     chkstraighttonew.checked = favorite.straighttonew; } 

and when clicks "save favorite", want add (or update) favorite details registry:

private void btnaddfavorite_click(object sender, eventargs e) {     string favoritename = cbofavorites.text;      if (string.isnullorempty(favoritename))     {         messagebox.show("please type name favorite in favorites box.");         return;     }      var favorite = new favorite(favoritename)     {         notes = txtnotes.text,         assesmenttoolversion = txtassettoolversion.text,         dblocation = txtdblocation.text,         notesfromplanner = chknotesfromplanner.checked,         projectcodes = txtprojectcodes.text,         project = cboprojects.text,         straighttonew = chkstraighttonew.checked     };      favorite.save();      // when saving favorite, add combo box     // (remove old 1 first if existed)     var existingfav = cbofavorites.items.cast<favorite>()         .firstordefault(fav => fav.name == favoritename);     if (existingfav != null)     {         cbofavorites.items.remove(existingfav);     }      cbofavorites.items.add(favorite);     cbofavorites.text = favoritename; } 

this should enough started, if want go registry route.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -