1

这是问题:

在我的设置中,我有两个名为“check1_State”和“check2_State”的布尔值。他们应该在表单加载时控制我的两个复选框。

这是表单加载时的代码:

checkBox1.Checked = Properties.Settings.Default.check1_State;
checkBox2.Checked = Properties.Settings.Default.check2_State; 

使用那段代码,只会读取 checkBox1。如果我评论第一行,第二行就可以了。

我设法让它在表单加载时放置一个计时器,但我想把它做对。这表明复选框实际上可以从设置中读取,但如果同时请求两个或更多,显然它不起作用。

知道为什么会这样吗?

4

1 回答 1

0

您可以使用表单级别标志来保持加载初始数据的状态

bool flag = false;

保存check1_State并且check2_State 当上面的标志是true

在表单加载事件中,从属性加载数据后设置标志

checkBox1.Checked = Properties.Settings.Default.check1_State;
checkBox2.Checked = Properties.Settings.Default.check2_State;
flag = true;

样本 :

public partial class Form1 : Form
{
    bool flag = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (flag)
        {
            //save settings 
        }
    }

    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (flag)
        {
            //save settings 
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        checkBox1.Checked = Properties.Settings.Default.check1_State;
        checkBox2.Checked = Properties.Settings.Default.check2_State;
        flag = true;
    }
}
于 2013-07-03T11:14:33.243 回答