如何设置所有复选框默认为未选中?

时间:2016-06-06 15:11:45

标签: c# .net winforms

我添加了一个带有一些复选框的新表单。 默认情况下,创建settings.txt文件时,不会向其写入任何内容。所以我得到一些错误,因为变量值只包含一个项目/索引,所以行

checkBox2.Checked = Convert.ToBoolean(Convert.ToInt32(values[1]));   

将给出超出范围的索引异常,以及行

checkBox1.Checked = Convert.ToBoolean(Convert.ToInt32(values[0]));

我想要做的是如果settings.txt文件为空,将所有复选框设置为false(未选中),并将settings.txt文件的每个复选框的值写为false / unchecked。

问题是,我只在事件中向文件写入值。

我还需要在构造函数public OptionsMenuForm()

中管理它
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Test
{
    public partial class OptionsMenuForm : Form
    {
        string settingsFile = Path.GetDirectoryName(Application.LocalUserAppDataPath)
            + "\\settings" + "\\settings.txt";


        public OptionsMenuForm()
        {
            InitializeComponent();

            string[] values = System.IO.File.ReadAllText(settingsFile).Split(',');
            checkBox1.Checked = Convert.ToBoolean(Convert.ToInt32(values[0]));
            if (Form1.files.Length == 0)
            {
                checkBox2.Checked = false;
                checkBox2.Enabled = false;
            }
            else
            {
                checkBox2.Checked = Convert.ToBoolean(Convert.ToInt32(values[1]));
            }
            checkBox3.Checked = Convert.ToBoolean(Convert.ToInt32(values[2]));
            checkBox4.Checked = Convert.ToBoolean(Convert.ToInt32(values[3]));

            checkBox1.CheckedChanged += CheckBoxesCheckedChanged;
            checkBox2.CheckedChanged += CheckBoxesCheckedChanged;
            checkBox3.CheckedChanged += CheckBoxesCheckedChanged;
            checkBox4.CheckedChanged += CheckBoxesCheckedChanged;
        }

        private void CheckBoxesCheckedChanged(object sender, EventArgs e)
        {
            StringBuilder sbValues = new StringBuilder();
            int i = 0;

            i = checkBox1.Checked ? 1 : 0;
            sbValues.Append(i.ToString() + ",");

            i = checkBox2.Checked ? 1 : 0;
            sbValues.Append(i.ToString() + ",");

            i = checkBox3.Checked ? 1 : 0;
            sbValues.Append(i.ToString() + ",");

            i = checkBox4.Checked ? 1 : 0;
            sbValues.Append(i.ToString() + ",");

            System.IO.File.WriteAllText(settingsFile, sbValues.ToString());
        }
    }
}

3 个答案:

答案 0 :(得分:1)

怎么样

var settings =  System.IO.File.ReadAllText(settingsFile);
if(settings =="")
{
    settings = "0,0,0,0,0";
}

string[] values = settings.Split(',');

答案 1 :(得分:1)

简单的方法:

this.checkBox1.Checked = false;
this.checkBox2.Checked = false;
this.checkBox3.Checked = false;
this.checkBox4.Checked = false;

更难的方式:

foreach (Control ctl in this.Controls)
{
    if (ctl is CheckBox) (ctl as CheckBox).Checked = false;
}

答案 2 :(得分:0)

根据我的理解你的settingsFile看起来像这样:

  

" A,B,C,d,E"

(a-e是个体设置)。 我首先检查字符串是否为空

string allText = System.IO.File.ReadAllText(settingsFile);
if (allText.Length == 0)
{
    // Set all checkBoxes to be unchecked
}
else
{
    string[] values = allTextSplit(',');
    // ......
}

这样就不会执行对字符串数组的引用,因为您之前检查了设置的长度,因此不会导致OutOfRange异常。

或者,我更喜欢的是:

string allText = System.IO.File.ReadAllText(settingsFile);
if (allText.Split(',').Length != 5)
{ // ... }

这样就可以检查从文件设置中获得的值的数量。