Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在UserControl c中保存复选框的状态#_C#_Winforms - Fatal编程技术网

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

C# 如何在UserControl c中保存复选框的状态#,c#,winforms,C#,Winforms,我正在尝试保存UserControl3上复选框的状态。如何做到这一点?在这里,我试图通过定时器保存它们。然后在UserControl3_加载上添加它们的保存 private void UserControl3_Load(object sender, EventArgs e) { materialCheckBox1.Checked = Properties.Settings.Default.CheckBox1; materialCh

我正在尝试保存UserControl3上复选框的状态。如何做到这一点?在这里,我试图通过定时器保存它们。然后在UserControl3_加载上添加它们的保存

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

这相当容易。使用
用户
作为范围

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

1.使用
CheckChanged/FormClosing
事件而不是计时器2。您的问题是什么?UserControl中没有FormClosing/CheckChanged事件。问题是它没有保存。设置的范围是什么?当您在
(应用程序设置)
下选择
复选框的
Checked
属性时,您创建的设置属性是bool类型。这不是一个表单。这是一个用户控件。所有材料复选框都存储在用户控件上。哦,对不起。它的工作原理相同。但是UserControls没有关闭事件。请使用OnHandleDestroyed事件。此时您可以完全访问控件的属性,因为它尚未被释放。如果您使用属性绑定到应用程序设置,则即使在控件的加载中也不需要这些代码。在当前解决方案中,设置属性的更改不会通知您,并且始终显示初始值,而与应用程序设置的数据绑定支持双向绑定。有关如何操作的更多信息,请参阅链接文章。