【发布时间】:2014-04-12 09:35:45
【问题描述】:
我使用 VSTO 内置设置文件来保存 Windows 应用程序设置。
我的 WinForm 上有一个复选框,而在加载表单时我读取了它的状态(选中或未选中) 来自设置文件中的相应属性。
只要我不退出应用程序,它就可以正常工作。
但是,当我退出然后再次执行应用程序时 - 设置不会从上次执行中保存,并且复选框状态不是最后的首选项。
我使用“用户”范围来保存设置。
在表单加载时,从设置中提取复选框状态。
private void MyFormLoad(object sender, EventArgs e)
{
//Find the appropriate property in the Settings file
System.Configuration.SettingsProperty property;
property = P_Settings.Settings.Default.Properties[checkBox.Name];
if (property != null)
checkBox.Checked = Convert.ToBoolean(property.DefaultValue);
}
在表单关闭时,将设置文件与表单状态同步。
private void ButtonApplyClick(object sender, EventArgs e)
{
System.Configuration.SettingsProperty property;
property = P_Settings.Settings.Default.Properties[checkBox.Name];
property.DefaultValue = checkBox.Checked.ToString();
P_Settings.Settings.Default.Save();
}
【问题讨论】: