问题描述
我有多种形式,使用称为UpdateTheme的相同方法来更改表单的背景颜色。我希望能够从另一种形式调用所有这些方法。
我试图用UpdateTheme方法创建一个基本表单,然后从基本表单继承所有其他表单,但是我不知道如何/如果有可能然后从一个单独的设置表单中调用派生表单方法的每个实例。
$ b $ pre $ public abstract class CustomForm:Form
{
public void UpdateTheme(string theme)
{
if(theme ==dark)
{
this.BackColor = Color.Black;
}
else if(theme ==light)
{
this.BackColor = Color.White;
code
$ b在设置表格中,有一些像
public void btnSetThemeToDark_Click(object sender,EventArgs e)
{
foreach派生形式)
{
derivedForm.UpdateTheme(dark);
最好的方法是做什么?
您可以创建一个名为StyleManager的单例,其中包含全局样式属性。这个单身人士有一个称为风格改变的事件,可以通过所有形式或基本形式进行处理。所以你所有的表单都可以从一个来源获得信息。
StyleManager
public class StyleManager
{
#region singleton
public static StyleManager Instance {get; } =新的StyleManager();
私人StyleManager()
{
}
#endregion
#地区事件
公共事件EventHandler覆盖styleChanged;
private void OnStyleChanged()
{
this.StyleChanged?.Invoke(this,EventArgs.Empty);
}
#endregion
#region属性
public Color BackColor {get;组; }
#endregion
#region methods
$ b public void UpdateBackColor(Color color)
{
this.BackColor =颜色;
this.OnStyleChanged();
}
#endregion
}
和在你的表单中使用它:
public Form()
{
this.InitializeComponent() ;
//附加到事件
StyleManager.Instance.StyleChanged + = this.StyleChanged;
}
//处理事件
private void StyleChanged(object sender,EventArgs eventArgs)
{
this.BackColor = StyleManager.Instance.BackColor;
}
//设置所有格式的背景颜色
StyleManager.Instance.UpdateBackColor(Color.Yellow);
I have multiple forms with the same method called "UpdateTheme" which changes the back colour of the form. I want to be able to call all of these methods from another form.
I tried to make a base form with the "UpdateTheme" method then have all other forms inherit from the base form, But I didnt know how/ if it was possible to then call every instance of the derived forms methods from a separate "Settings" form.
public abstract class CustomForm : Form
{
public void UpdateTheme(string theme)
{
if (theme == "dark")
{
this.BackColor = Color.Black;
}
else if (theme == "light")
{
this.BackColor = Color.White;
}
}
}
In the settings form I would have something like
public void btnSetThemeToDark_Click(object sender, EventArgs e)
{
foreach (instance of derived form)
{
derivedForm.UpdateTheme("dark");
}
}
Whats the best way to do this?
You could create a singleton called StyleManager that contains the global style properties. This singleton has an event called style changed that can be handled by all forms, or a base form. So all of your forms get the information from one source.
StyleManager
public class StyleManager
{
#region singleton
public static StyleManager Instance { get; } = new StyleManager();
private StyleManager()
{
}
#endregion
#region events
public event EventHandler StyleChanged;
private void OnStyleChanged()
{
this.StyleChanged?.Invoke(this, EventArgs.Empty);
}
#endregion
#region properties
public Color BackColor { get; set; }
#endregion
#region methods
public void UpdateBackColor(Color color)
{
this.BackColor = color;
this.OnStyleChanged();
}
#endregion
}
and use it in your forms like this:
public Form()
{
this.InitializeComponent();
//Attach to the event
StyleManager.Instance.StyleChanged += this.StyleChanged;
}
//Handle event
private void StyleChanged(object sender, EventArgs eventArgs)
{
this.BackColor = StyleManager.Instance.BackColor;
}
//set backcolor of all forms
StyleManager.Instance.UpdateBackColor(Color.Yellow);
这篇关于如何从另一个窗体调用多个方法C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!