Dino Velić
Dino Velić

Reputation: 898

C# Reloading form

When user changes background color for example, the Settings.settings file is modified. And it works.

But the application doesn't change it's background color after user clicks OK. It works only when I close and build the application again.

How can I reload my form or user control on button click? (Tried with .Refresh(), but it doesn't work)

    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;
        }
    }

Upvotes: 0

Views: 2091

Answers (4)

Sylca
Sylca

Reputation: 2545

try this, this changes background color of form in color that you chose from 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);
        }
    }

Upvotes: 0

Justin Harvey
Justin Harvey

Reputation: 14682

Run whatever code you have that sets the control's properties at start up from the settings file.

e.g.

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

Upvotes: 1

AgentFire
AgentFire

Reputation: 9790

You can create binding for it. With a little tricks the binding can even allow the immediate interface language switching.

Upvotes: 0

CSharpDev
CSharpDev

Reputation: 869

On the button click event, just load the backcolor from your settings file. Something like:

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

Upvotes: 0

Related Questions