【问题标题】:Is there a more efficient way of saving checkboxes checked state?有没有更有效的方法来保存复选框选中状态?
【发布时间】:2020-06-06 21:01:09
【问题描述】:

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

所以,按照说明,我转到项目的属性,转到设置并创建了一个类型为“bool”的新设置,范围“用户”和值“假”。然后,我创建了 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 循环 遍历复选框,但它显然没有按预期工作;现在我已经尝试了很多我只是不记得的事情。我已经搜索和搜索,但还没有找到方法,也许我不知道要寻找什么,因为我处于非常初级的水平。

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

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

编辑: 我在流布局面板中有所有复选框,我尝试循环遍历每个框的是:

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

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

【问题讨论】:

  • 为什么需要在启动时访问所有控件?使用表单设计器设置所需的值,然后根据您的喜好使用每组进行编译。
  • 使用应用程序设置绑定。
  • 好吧,我建议您遍历每个复选框,但您说您已经尝试过了,“但它显然没有按预期工作”。我设想的代码非常简单。您明显错误的代码是什么样的?也许您可以将其显示为minimal reproducible example
  • 嗨@Flydog57 我在问题的编辑部分添加了我试图使用的循环
  • 嗨@RezaAghaei 我读到了这个并尝试了它,但我不记得了,愚蠢的我没有保存我在任何地方所做的事情,但我知道它做了一些奇怪的事情,比如检查每个复选框时点击其中一个

标签: c# visual-studio winforms save settings


【解决方案1】:

也许您可以尝试将设置保存在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

最后一步,您可以使用以下代码获取/保存设置。

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

【讨论】:

  • 嘿!非常感谢,这有效!我想做这样的事情,但从来不明白所有这些 xml 的东西是如何工作的,现在我做了,我可以继续这个项目,因为我真的被困在那里。非常感谢!
【解决方案2】:

您可以使用 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();

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

【讨论】:

    猜你喜欢
    • 2011-05-05
    • 2014-04-17
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 2011-12-31
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多