C# 如何在UserControl c中保存复选框的状态#
我正在尝试保存UserControl3上复选框的状态。如何做到这一点?在这里,我试图通过定时器保存它们。然后在UserControl3_加载上添加它们的保存C# 如何在UserControl c中保存复选框的状态#,c#,winforms,C#,Winforms,我正在尝试保存UserControl3上复选框的状态。如何做到这一点?在这里,我试图通过定时器保存它们。然后在UserControl3_加载上添加它们的保存 private void UserControl3_Load(object sender, EventArgs e) { materialCheckBox1.Checked = Properties.Settings.Default.CheckBox1; materialCh
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事件。此时您可以完全访问控件的属性,因为它尚未被释放。如果您使用属性绑定到应用程序设置,则即使在控件的加载中也不需要这些代码。在当前解决方案中,设置属性的更改不会通知您,并且始终显示初始值,而与应用程序设置的数据绑定支持双向绑定。有关如何操作的更多信息,请参阅链接文章。