如何从一种形式更改所有其他形式的背景颜色? [英] How can we change the background color of all other forms from one form?
问题描述
如何从一种形式(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:
此新设置是在用户范围中创建的.
用户范围中的所有设置都是按用户应用的,可以更改.
应用程序范围"中的设置被认为是只读的.
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:
为所有更改此设置后应更改其BackColor的窗体分配相同的设置.
当然,您可以将通用设置分配给任何其他控件的任何其他属性.
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
.
您可以为所有窗体的BackColor设置一个新值,以更改设置的值:
(所有打开的共享BackGround
Color相同设置的表单将立即更改颜色)
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屋!