2

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"];
            }
        }
    }
Zer01
  • 41
  • 6
  • a.) That doesn't sound like something that should go into application settings. b.) Try it without the debugger/recompiling. You may be fooled by the system that puts the userdefined settings always into a new folder when recompilling the app (because of version number change etc.). – Ralf Nov 07 '17 at 16:01
  • How are you reading those settings back in? – BugFinder Nov 07 '17 at 16:03
  • @Ralf, I already created an installer project and installed the application. Its the same. I will edit the question and add how I load the settings. – Zer01 Nov 07 '17 at 16:07

1 Answers1

2

If I got the problem correct, save the Settings into an external file like xml or a simple txt. After starting the Programm, the saved Settings will be read by your program (you must code a restorefunction) and all saved settings are active.

the easiest way is to handle this with a Streamreader.

Exitare
  • 561
  • 2
  • 10
  • 30