我有两种表格:
MainFormSettingsForm可以想象,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发布于 2012-09-13 14:54:15
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();
}https://stackoverflow.com/questions/12408969
复制相似问题