gpt4 book ai didi

c# - 更改所有 Windows Forms 窗体上的设置

转载 作者:行者123 更新时间:2023-11-30 15:32:16 27 4
gpt4 key购买 nike

我已经更改了我的 app.config 文件以允许用户更改程序的配色方案。我可以弄清楚如何更改他们在其中更改这些设置的表单的背景颜色:

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

但是我怎样才能改变我所有表格的背景颜色呢?这就像我仍然想将我所有的表单传递给一个函数。我已经问过这个问题,有人告诉我使用 app.config 文件。既然我已经这样做了,我是不是用错了?

最佳答案

只是您需要一个基本表单,您项目中的所有表单都必须继承:

public class FormBase : Form {
protected override void OnLoad(EventArgs e){
Color colBackColor = Properties.Settings.Default.basicBackground;
BackColor = colBackColor;
}
}
//Then all other forms have to inherit from that FormBase instead of the standard Form
public class Form1 : FormBase {
//...
}
public class Form2 : FormBase {
//...
}

更新

public interface INotifyChangeStyle {
void ChangeStyle();
}
public class FormBase : Form, INotifyChangeStyle {
protected override void OnLoad(EventArgs e){
ChangeStyle();
}
public void ChangeStyle(){
//Perform style changing here
Color colBackColor = Properties.Settings.Default.basicBackground;
BackColor = colBackColor;
//--------
foreach(var c in Controls.OfType<INotifyChangeStyle>()){
c.ChangeStyle();
}
}
}
public class MyButton : Button, INotifyChangeStyle {
public void ChangeStyle(){
//Perform style changing here
//....
//--------
foreach(var c in Controls.OfType<INotifyChangeStyle>()){
c.ChangeStyle();
}
}
}
//... the same for other control classes

关于c# - 更改所有 Windows Forms 窗体上的设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19337392/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com