简体   繁体   中英

How to call multiple methods from another form C#

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

Assuming this forms are MdiChildren of the form, you would do it this way:

foreach (var form in this.MdiChildren)
{
    var castedForm = form as CustomForm;
    if (myObjRef != null)
    {
         castedForm.UpdateTheme("dark");
    }        


}

This functionality works beyond themes to any common method of the child forms.

I don't think this is the best way to do this. But, you can archive what you want by using this code.

CustomForm mainFrm = (CustomForm)Application.OpenForms["YouCustomFormNameHere"];
mainFrm.UpdateTheme("dark");

Replace ' YouCustomFormNameHere ' with your CustomForm form name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM