首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Properties.Settings -多种形式

Properties.Settings -多种形式
EN

Stack Overflow用户
提问于 2012-09-13 14:48:03
回答 1查看 784关注 0票数 1

我有两种表格:

  • MainForm
  • SettingsForm

可以想象,MainForm使用的值,如Properties.Settings.Default.PathSettingsForm,应该能够在运行时配置这样的值。

但是SettingsForm: Properties.Settings.Default.Save();在应用程序重新启动后就会生效,尽管我要在MainForm: Properties.Settings.Default.Reload();中重新加载这些设置

到目前为止,我有这样的看法:

MainForm.cs

代码语言:javascript
复制
    // 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

代码语言:javascript
复制
    // 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。因此,在最后,我将按照以下命令执行函数:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-13 14:54:15

代码语言:javascript
复制
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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12408969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档