我们如何从一种表单更改所有其他表单的背景颜色? [英] How can we change the background color of all other forms from one form?

查看:42
本文介绍了我们如何从一种表单更改所有其他表单的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从一个表单 (settings.form) 更改所有其他表单的背景颜色?我想开发我的毕业项目.它是一个社交媒体桌面管理项目.我想用一个切换器将所有表单更改为暗模式.我怎样才能做到这一点?这是我的 settings.cs

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.

推荐答案

您可以使用 (ApplicationSettings) 属性,该属性可从表单设计器的 Properties 面板访问.展开 ApplicationSettings,打开 PropertyBinding 对话框,将设置添加到 BackColor 属性(例如,CommonFormBackColor)和对所有表单使用相同的设置.

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.

您可以直接在应用程序设置的 PropertyBinding 对话框中创建设置:

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

这个新设置是在用户范围中创建的.
User Scope 中的所有设置都基于每个用户应用并且可以更改.
Application Scope 中的设置被视为只读.

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.

新设置将出现在 ApplicationSettings 可扩展属性下:

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

为所有应在更改此设置时更改其背景颜色的表单分配相同的设置.
您当然可以为任何其他控件的任何其他属性分配一个公共设置.

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.

在运行时更改设置值时,所有打开的表单 - 以及稍后打开的表单 - 将呈现相同的 BackColor.

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

您可以通过更改设置的值为所有表单的背景色设置新值:
(所有打开的表单共享相同的 BackGround 颜色设置将立即改变颜色)

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

这篇关于我们如何从一种表单更改所有其他表单的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆