1

What is the best way of loading many user settings at once. For example my application has many user settings like :

settingCapitalizeStrings bool user true

then in my FormOptions_Load I need to loop through all settings and values like :

private void FormOptions_Load(object sender, EventArgs e)
{
    if(s.settingCapitalizeStrings == true)
    {
       capitalizeStringsCheckBox.Checked = true;
    }
    // and many many more
}

and then handle every CheckBox_CheckedChanged

Is there better solution ? Can I bind user settings or something like that?

Carlo
  • 151
  • 1
  • 3
  • 16
  • Are you not using the config file features build into .net? – hatchet - done with SOverflow Feb 07 '14 at 19:31
  • What is it that you exactly want to acheive with this? Depending on your needs you can either use the config file as suggested by @hatchet r you could possibly create an extension method that takes a couple of parameters and use that? – jacqijvv Feb 07 '14 at 19:32
  • Seems like he wants a per user setting, not a global setting. App/web.config would only work if ALL users wanted the setting to be the same. – Scottie Feb 07 '14 at 19:41
  • See http://stackoverflow.com/questions/744746/best-way-to-save-per-user-options-in-c-sharp and http://stackoverflow.com/questions/909688/what-is-the-difference-between-app-config-file-and-xyz-settings-file – hatchet - done with SOverflow Feb 07 '14 at 19:50

2 Answers2

3

I suggest to use Application Settings:

  1. Select checkbox in designer
  2. Go to properties and select (ApplicationSettins)
  3. Go to Checked, click on its value and select new
  4. Add new application setting scoped to user

enter image description here

That will generate code which loads setting from file and adds binding to Checked property of checkbox (that will load property value from settings file during form creation and update settings file when it checkbox Checked property value will be changed):

this.capitalizeStringsCheckBox.Checked = global::WindowsFormsApplication.Properties.Settings.Default.CapitalizeStrings;
this.capitalizeStringsCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::WindowsFormsApplication.Properties.Settings.Default, "CapitalizeStrings", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 1
    This is it. Thanks. I'm using `DevExpress CheckEdit` so I could not find that. With `native CheckBox` everything works well. – Carlo Feb 07 '14 at 19:42
  • 1
    When using `DevExpress CheckEdit` bind `EditValue` property to application setting. – Carlo Feb 07 '14 at 19:57
1

Try to have a look at Configuration, and put everything in app.config: http://msdn.microsoft.com/en-us/library/system.configuration.configuration(v=vs.110).aspx

Also,

private void FormOptions_Load(object sender, EventArgs e)
{
    capitalizeStringsCheckBox.Checked = s.settingCapitalizeStrings;
}

is much more elegant.

Charles F
  • 114
  • 1
  • 5