I have over 100 checkboxes in my program and I want to save the state of these programatically without creating settings property manualy in visual studio. The checkboxes are bind to a user control including a nummericUpDownBox.
This is how I save it:
//Save Button Click
private void button2_Click(object sender, EventArgs e)
{
Settings.Default.Reset();
foreach (Control c in panel1.Controls)
{
Settings.Default.Properties.Remove(c.Name);
Settings.Default.Properties.Remove(c.Name + "value");
if ((c is checkNum) && Settings.Default.Properties[c.Name] == null)
{
SettingsProperty property = new SettingsProperty(c.Name);
property.DefaultValue = false;
property.IsReadOnly = false;
property.PropertyType = typeof(bool);
property.Provider = Settings.Default.Providers["LocalFileSettingsProvider"];
property.Attributes.Add(typeof(UserScopedSettingAttribute), new UserScopedSettingAttribute());
Settings.Default.Properties.Add(property);
Settings.Default[c.Name] = ((checkNum)c).Checked;
SettingsProperty property2 = new SettingsProperty(c.Name + "value");
property2.DefaultValue = 2;
property2.IsReadOnly = false;
property2.PropertyType = typeof(int);
property2.Provider = Settings.Default.Providers["LocalFileSettingsProvider"];
property2.Attributes.Add(typeof(UserScopedSettingAttribute), new UserScopedSettingAttribute());
Settings.Default.Properties.Add(property2);
Settings.Default[c.Name + "value"] = Convert.ToInt32(((checkNum)c).Value);
}
}
Settings.Default.Save();
}
Now, it is saved, and if I change the Settings again, I can restore the Settings. But if I close the application It doesnt work. It doesnt save the new Setting when Application is closed an restart. So how can I save the settings permanently? So that I can load it after restart the application. What do I have to do?
This is how I load the Settings:
//Load Button Click
private void button4_Click(object sender, EventArgs e)
{
foreach (Control c in panel1.Controls)
{
if ((c is checkNum) && Settings.Default.Properties[c.Name] != null)
{
((checkNum)c).Checked = (bool)Settings.Default[c.Name];
((checkNum)c).Value = (int)Settings.Default[c.Name + "value"];
}
}
}