我有两种形式:
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()
使用Properties.Settings.Default.Path
。所以最后我要按这个顺序执行函数:
ReloadLog(); // Triggered by the user (several times)
// This reloads contents of log, say C:\\main.log
configStatusLabel_Click(); // User hit "configure button", there are two active forms
// SettingsForm is now displayed too
// At this point ReloadLog() may be called in MainForm many times
// Meanwhile in SettingsForm:
Properties.Settings.Default.Path = PathTextBox.Text;
private void saveButton_Click(object sender, EventArgs e) // User hit save button
{
Properties.Settings.Default.Save();
Close(); // This will trigger form_FormClosed in main form
}
// Now you would expect that following line will open D:\\another.log
ReloadLog();
// But it still uses original config, however when I turn app off and on again, it works