I don't think the DefaultValue
is where you should store the value for a settings property. That looks more like a part of the property definition (which should stay constant across application runs and thus be hard-coded) rather than like something that will be saved along with the settings.
Instead, try directly using the indexer of the settings object:
Loading:
object propValue = P_Settings.Settings.Default[checkBox.Name];
if (propValue != null) {
checkBox.Checked = Convert.ToBoolean(propValue);
}
Saving:
P_Settings.Settings.Default[checkBox.Name] = checkBox.Checked.ToString();
P_Settings.Settings.Default.Save();
EDIT: Originally, you were storing the checkbox state in the DefaultValue
property of the settings property. The DefaultValue
is meant to provide a default value that will be returned if no settings value was found in the stored settings. It is not a value that will be stored with the settings, as it is not supposed to be user-defined, or changed during the application's run.
Therefore, what you tried before resulted in the observed behavior: You could assign a value to DefaultValue
, which would stay for as long as the application was active, but that value was not stored upon P_Settings.Settings.Default.Save()
or restored upon application startup, so upon the next launch of your application, DefaultValue
would have its default value (probably null
) again.