如何在UserControl中保存复选框的状态c#

时间:2017-03-27 00:28:31

标签: c# winforms

我正在尝试保存UserControl3上的复选框状态。怎么办?在这,我试图通过计时器保存它们。然后在UserControl3_Load上添加他们的保存。

private void UserControl3_Load(object sender, EventArgs e)
        {

            materialCheckBox1.Checked = Properties.Settings.Default.CheckBox1;
            materialCheckBox2.Checked = Properties.Settings.Default.CheckBox2;
            materialCheckBox4.Checked = Properties.Settings.Default.CheckBox3;
        }

private void timer1_Tick(object sender, EventArgs e)
        {
            Properties.Settings.Default.CheckBox1 = materialCheckBox1.Checked;
            Properties.Settings.Default.CheckBox2 = materialCheckBox2.Checked;
            Properties.Settings.Default.CheckBox3 = materialCheckBox4.Checked;
            Properties.Settings.Default.Save();
        }

1 个答案:

答案 0 :(得分:0)

这很容易。使用User作为范围。

enter image description here

UserControl1.cs:

    public UserControl1()
    {
        InitializeComponent();
    }

    private void UserControl1_Load(object sender, EventArgs e)
    {
        checkBox1.Checked = Properties.Settings.Default.CheckBox1;
        checkBox2.Checked = Properties.Settings.Default.CheckBox2;
    }

    protected override void OnHandleDestroyed(EventArgs e)
    {
        Properties.Settings.Default.CheckBox1 = checkBox1.Checked;
        Properties.Settings.Default.CheckBox2 = checkBox2.Checked;
        Properties.Settings.Default.Save();
        base.OnHandleDestroyed(e);
    }