Question

I have two forms:

  • MainForm
  • SettingsForm

As you can imagine, MainForm uses values such as Properties.Settings.Default.Path and SettingsForm should be able to configure such a value on runtime.

But somehow SettingsForm: Properties.Settings.Default.Save(); takes effect just after application restart, although I'm reloading those settings in MainForm: Properties.Settings.Default.Reload();

I have this so far:

In 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)

And 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();
    }

What am I missing? How can I achieve "on demand change"?


More on program flow

In main form, there are several buttons which will trigger function such as ReloadLog() which uses Properties.Settings.Default.Path. So at the end I'm having functions executed at this order:

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
Was it helpful?

Solution

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top