Search code examples
c#.netvisual-studiowinformsapplication-settings

How can we change the background color of all other forms from one form?


How can we change the background color of all other forms from one form (settings.form)? I want to develop my graduation project. Its a social media desktop management project. I want to change all of the forms to dark mode with one switcher. How can I do that? This my settings.cs

public void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        panel1.BackColor= Color.FromArgb(34, 36, 49);
        form1.BackColor = Color.FromArgb(34, 36, 49);
        form2.BackColor = Color.FromArgb(34, 36, 49);
        this.BackColor = Color.FromArgb(34, 36, 49);
        this.label1.BackColor = Color.White;
        this.label1.ForeColor = Color.FromArgb(34, 36, 49);
    }
    else
    {
        this.BackColor = Color.White;
        this.label1.BackColor = Color.FromArgb(34, 36, 49);
        this.label1.ForeColor = Color.White;
    }
    form1.Show();
    form1.Refresh();
    form2.Show();
    form2.Refresh();

All background color is changing when I switch. But all forms is opening sametime.


Solution

  • You can use the (ApplicationSettings) property, accessible from the Form Designer's Properties panel. Expand ApplicationSettings, open up the PropertyBinding dialog, add a Setting to the BackColor property (e.g., CommonFormBackColor) and use the same setting for all Forms.

    You can create the Setting directly in the Application Settings' PropertyBinding dialog:

    Properties Application Settings New

    This new Setting is created in the User Scope.
    All settings in the User Scope are applied on a per-User basis and can be changed.
    Settings in the Application Scope are considered read-only.

    Properties Application Settings Add

    The new Setting will then appear under the ApplicationSettings expandable property:

    Properties Application Settings

    Assign the same Setting to all Forms that should change their BackColor when this setting is changed.
    You can of course assign a common Setting to any other Property of any other Control.

    The use of a Form Template (or a base Form class) can automate the whole process.

    When the Setting value is changed at run-time, all opened Forms - and those that will be opened later - will present the same BackColor.

    You can set a new value to all Form's BackColor changing the Settings's Value:
    (all opened Form that share the same Setting for the BackGround Color will change color immediately)

    Properties.Settings.Default.CommonFormBackColor = Color.Orange;
    

    You can save the current Settings selection (to preserve the value assigned in the current session, so it will be used again when the application is restarted) with:

    Properties.Settings.Default.Save();
    

    You can reset the default value (the value originally assigned to the Settings in the Designer) calling:

    Properties.Settings.Default.Reset();