【问题标题】:C# Reloading formC# 重新加载表单
【发布时间】:2012-09-03 08:31:47
【问题描述】:

例如,当用户更改背景颜色时,会修改 Settings.settings 文件。它有效。

但在用户单击确定后,应用程序不会更改其背景颜色。 它仅在我关闭并再次构建应用程序时才有效。

如何在单击按钮时重新加载表单或用户控件? (尝试使用 .Refresh(),但它不起作用)

    private void refreshSettings()
    {
        this.BackColor = Properties.Settings.Default.bgdColor;
        this.Font = Properties.Settings.Default.fontType;
        this.ForeColor = Properties.Settings.Default.fontColor;
    }

    private void Settings_Load(object sender, EventArgs e)
    {
        refreshSettings();
        bgdColorLBL.BackColor = Properties.Settings.Default.bgdColor;
        fontColorLBL.BackColor = Properties.Settings.Default.fontColor;
        fontTypeLBL.Font = Properties.Settings.Default.fontType;
        fontTypeLBL.Text = Properties.Settings.Default.fontType.Name;
    }

    private void okBTN_Click(object sender, EventArgs e)
    {
        LeagueUC lg = new LeagueUC();
        InitializeComponent();
        this.Close();
    }

    private void bgdColorLBL_Click(object sender, EventArgs e)
    {
        ColorDialog dlg = new ColorDialog();
        dlg.Color = Properties.Settings.Default.bgdColor;

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            Properties.Settings.Default.bgdColor = dlg.Color;
            Properties.Settings.Default.Save();
            bgdColorLBL.BackColor = dlg.Color;
        }
    }

【问题讨论】:

  • 给我们看一些代码。也许只有读取设置并在表单加载时应用颜色的实现才真正起作用。
  • 如果你需要重绘你试过this.Invalidate()吗?
  • 尝试调用 InitializeComponent()
  • 显示一些代码,因为未知数太多。加上“It works only when I close and build the application again”——build 是什么意思?重新编译?
  • @mortb 我添加了一些代码。布拉德和阿特伯德它不起作用。 aleksey.berezan 是的,重新编译

标签: c# winforms forms reload


【解决方案1】:

试试这个,这会改变您从 ColorDialog 中选择的颜色的表单的背景颜色:

    private void button2_Click(object sender, EventArgs e)
    {
        ColorDialog dlg = new ColorDialog();

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            this.BackColor = System.Drawing.Color.FromName(dlg.Color.Name);
        }
    }

【讨论】:

    【解决方案2】:

    在启动时从设置文件运行设置控件属性的任何代码。

    例如

        private void bgdColorLBL_Click(object sender, EventArgs e) 
    { 
        ColorDialog dlg = new ColorDialog(); 
        dlg.Color = Properties.Settings.Default.bgdColor; 
    
        if (dlg.ShowDialog() == DialogResult.OK) 
        { 
            Properties.Settings.Default.bgdColor = dlg.Color; 
            Properties.Settings.Default.Save(); 
    
            Settings_Load(null, null);
        } 
    } 
    

    【讨论】:

    • 启动到底是什么意思?
    • 见上面的编辑。然后我可能会考虑重构它,这样您就不必使用 (null, null) 参数调用它。
    • 好吧,它看起来可以工作,但我不能在另一个表单上调用它,因为它在那里不存在。知道为什么吗?
    • Settings_Load 是一种私有方法。如果您想从其他地方调用它,则需要将其公开。
    • 哦,天哪,我认为这是一些已实现的功能。好的,谢谢!
    【解决方案3】:

    您可以为它创建binding。通过一些小技巧,绑定甚至可以允许即时界面语言切换。

    【讨论】:

    • @DinoVelić 恐怕我没有。
    【解决方案4】:

    在按钮单击事件中,只需从您的设置文件中加载背景颜色。比如:

    this.BackColor = Properties.Settings.Default.Color;
    

    【讨论】:

      猜你喜欢
      • 2017-02-18
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 2017-10-02
      • 2012-05-22
      • 1970-01-01
      相关资源
      最近更新 更多