0

私は2つのフォームを持っています:

  • MainForm
  • SettingsForm

ご想像のとおり、は次のような値をMainForm使用し、実行時にそのような値Properties.Settings.Default.PathSettingsForm構成できるはずです。

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()which usesなどの機能をトリガーするいくつかのボタンがあります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
4

1 に答える 1

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();
}
于 2012-09-13T14:54:15.213 に答える