Domanda

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?

È stato utile?

Soluzione

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));

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top