我有两种形式:
MainForm
SettingsForm
可以想象,MainForm
使用Properties.Settings.Default.Path
和SettingsForm
之类的值应该能够在运行时配置这样的值。
但不知怎的,SettingsForm: Properties.Settings.Default.Save();
在应用程序重启后才生效,虽然我正在MainForm: Properties.Settings.Default.Reload();
重新加载这些设置
到目前为止,我有这个:
在MainForm.cs
:
// Handles "config button click" => display settings form
private void configStatusLabel_Click(object sender, EventArgs e)
{
SettingsForm form = new SettingsForm();
form.FormClosed += new FormClosedEventHandler(form_FormClosed);
form.Show();
}
// Callback triggered on Settings form closing
void form_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.Reload();
}
// There are another methods called after form_FormClosed is triggered, for example
// StremWriter = new StreamWriter( Properties.Settings.Default.Path)
SettingsForm.cs
:
// Triggered on "Save button click" in Settings form, after changing values
// Example: Properties.Settings.Default.Path = "C:\\file.txt"
private void saveButton_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
Close();
}
我错过了什么? 如何实现“按需更改”?
在主窗体中,有几个按钮会触发使用ReloadLog()
的{{1}}等功能。所以最后我按照这个顺序执行了函数:
Properties.Settings.Default.Path
答案 0 :(得分:1)
private void configStatusLabel_Click(object sender, EventArgs e)
{
SettingsForm form = new SettingsForm();
form.FormClosed += new FormClosedEventHandler(form_FormClosed);
form.FormClosed += (s, e) => { MethodThatAppliesTheSettings(); };
form.Show();
}