Junaid
Junaid

Reputation: 639

Changing background color of all forms in winform application

I have BaseForm Class like this that is inheriting Form Class

 public partial class BaseForm : Form
 {
    protected override void OnLoad(EventArgs e)
    {
       Color colBackColor =Properties.Settings.Default.FormsBackgroundColor;
       BackColor = colBackColor;
    }
  }

and MainForm class like this which is inheriting BaseForm Class.

public partial class MainForm : BaseForm
{
    private void button1_Click_1(object sender, EventArgs e)
    {
            ColorDialog colorDlg = new ColorDialog();
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.FormsBackgroundColor= colorDlg.Color;
                Properties.Settings.Default.Save();
                this.Refresh();
                this.Invalidate();
            }
        }    
 }

When i click button1 on MainForm and choose color from the color dialog. The background color of MainForm doesn't change. What I am doing wrong?

Btw color changes when i re-run the application.

Upvotes: 2

Views: 7595

Answers (1)

Patrick McDonald
Patrick McDonald

Reputation: 65411

The OnLoad event is only triggered when the form loads, it doesn't get triggered when you click the button. So you need to change the form BackColor in button1_Click_1 also.

if (colorDlg.ShowDialog() == DialogResult.OK)
{
    Properties.Settings.Default.FormsBackgroundColor= colorDlg.Color;
    Properties.Settings.Default.Save();
    this.BackColor = colorDlg.Color;
}

Upvotes: 3

Related Questions