有没有更有效的方法来保存复选框的选中状态?

Is there a more efficient way of saving checkboxes checked state?

所以我正在制作一个清单应用程序,我想在其中包含一个 "Save" 按钮来保存所有复选框的选中状态 (在 Visual Studio 2019 和 c# 中) 。现在,我有 65 个复选框(每个任务一个),但将来会有更多。

因此,按照说明,我转到项目的 属性 ,然后转到 设置 并创建了一个 新设置,类型 "bool",范围 "user",值 "False"。然后,我创建了我的 GetSettings() 函数,如下所示:

public void GetSettings()
    {
        checkBox1.Checked = Properties.Settings.Default.CheckBox1;
    }

和像这样的 SaveSettings():

public void GetSettings()
        {
            Properties.Settings.Default.CheckBox1 = checkBox1.Checked;
            Properties.Settings.Default.Save();
        }

然后我将 SaveSettings()GetSettings() 添加到 SaveButton_Click,以及 GetSettings()Form1_Load

这显然很有魅力,对于具有 5 个复选框的更简单的表单,我不介意为每个复选框添加一个新设置并在函数中声明它,但是如果有 65 个以上的复选框,工作就会变得有点丰富。

我尝试使用 foreach 循环 循环检查复选框,但它显然没有按预期工作;现在我已经尝试了很多我不记得的事情。我找了又找都没有找到办法,也许我不知道该找什么,因为我是初学者。

无论如何,有谁知道保存我的复选框状态并在我下次打开应用程序时加载的更有效方法吗?只要有效,任何方法都可以(而且我可能会学到一些新东西)。

提前谢谢你,如果你需要更多信息或者我没有解释清楚,我很乐意回复!

编辑: 我在 Flow Layout Panel 中有所有复选框,我尝试循环遍历每个框的是:

foreach (CheckBox chk in flowLayoutPanel1.Controls)
    {
        Properties.Settings.Default.CheckBox1 = chk.Checked;
        Properties.Settings.Default.Save();
    }

以及获取此类设置:

foreach (CheckBox chk in flowLayoutPanel1.Controls)
    {
        chk.Checked = Properties.Settings.Default.CheckBox1;
    }

但是,当我单击“保存”按钮时,所做的就是让它们全部选中或取消选中。也许我做错了!

您可以使用 ResXResourceWriter 和 -Reader 类

// Save
var rw = new ResXResourceWriter(<your file>);

foreach (CheckBox box in Form.Controls)
{
    rw.AddResource(box.ToString(), box.Checked);
}

rw.Close();

// Read
var rr = ResXResourceReader(<your file>);

foreach (DictionaryEntry entry in rr)
{
    var box = Form.Controls
               .First(x => x.ToString() == entry.Key.ToString());
    box.Checked = (bool)entry.Value;
}

rr.Close();

这可能有一些错误,我没有时间测试它

也许您可以尝试将设置保存在App.config 文件中。

首先,您可以像这样添加节点appSettings

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>

    <appSettings>
        <add key="CheckBox1" value="True" />
        <add key="CheckBox2" value="True" />
        <add key="CheckBox3" value="False" />
    </appSettings>

    <!--...-->
</configuration>

然后添加引用System.Configuration.

最后一步,可以使用下面的代码get/save设置。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

private void btGetSettings_Click(object sender, EventArgs e)
{
    foreach(Control control in this.Controls)
    {
        // Check if is CheckBox
        if (control is CheckBox)
        {
            // Read the setting in App.config
            string value = config.AppSettings.Settings[control.Name].Value;
            ((CheckBox)control).Checked = Convert.ToBoolean(value);
        }
    }
    // Save and refresh settings
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
}

private void btSaveSettings_Click(object sender, EventArgs e)
{
    foreach (Control control in this.Controls)
    {
        if (control is CheckBox)
        {
            // Modify the setting in App.config
            config.AppSettings.Settings[control.Name].Value = ((CheckBox)control).Checked.ToString();
        }
    }
    // Save and refresh settings
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
}