How to change MetroFramework style color to all control

Sunil Acharya picture Sunil Acharya · Jan 20, 2016 · Viewed 28.5k times · Source

I'm using MetroFramework in my desktop application and set all the themes color ad default while making the change in parent form I want to update all child form and control color as par themes color.

Check the design UI

http://thielj.github.io/MetroFramework

Change Themes

var m = new Random();
int next = m.Next(0, 13);
this.Style = (MetroColorStyle)next;

With the action the main form color is changing but the controler and child form style color is not changing.

Answer

Fatih GÜRDAL picture Fatih GÜRDAL · Aug 6, 2016

hHi Sunil,

Exmple -1 Toolbox in MetroStyleManager add

Add StyleManager Set Setting

Example 2 (Extension Method)

    public static void SetDefaultStyle(this IContainer contr, MetroForm owner, MetroColorStyle style)
    {
        MetroStyleManager manager = FindManager(contr, owner);
        manager.Style = style;
    }
    public static void SetDefaultTheme(this IContainer contr, MetroForm owner, MetroThemeStyle thme)
    {
        MetroStyleManager manager = FindManager(contr, owner);
        manager.Theme = thme;
    }
    private static MetroStyleManager FindManager(IContainer contr, MetroForm owner)
    {
        MetroStyleManager manager = new MetroStyleManager(contr);
        foreach (IComponent item in contr.Components)
        {
            if (((MetroStyleManager)item).Owner == owner)
            {
                 manager = (MetroStyleManager)item;
            }
        }
        return manager;
    }

Using:

    public frmMain()
    {
        InitializeComponent();            
        this.components.SetDefaultStyle(this, MetroColorStyle.Purple);
    }

Exemple - 3: If you want to set the theme for all forms.

Step 1: Create new class "MyExtensions.cs". This is content:

public static class MyExtensions
{
    //What is your style
    private const MetroColorStyle FormStyle = MetroColorStyle.Green;
    public static void SetStyle(this IContainer container, MetroForm ownerForm)
    {
        if (container == null)
        {
            container = new System.ComponentModel.Container();
        }
        var manager = new MetroFramework.Components.MetroStyleManager(container);
        manager.Owner = ownerForm;
        container.SetDefaultStyle(ownerForm, FormStyle);


    }
    public static void SetDefaultStyle(this IContainer contr, MetroForm owner, MetroColorStyle style)
    {
        MetroStyleManager manager = FindManager(contr, owner);
        manager.Style = style;
        owner.Style = style;
    }
    public static void SetDefaultTheme(this IContainer contr, MetroForm owner, MetroThemeStyle thme)
    {
        MetroStyleManager manager = FindManager(contr, owner);
        manager.Theme = thme;
    }
    private static MetroStyleManager FindManager(IContainer contr, MetroForm owner)
    {
        MetroStyleManager manager = null;
        foreach (IComponent item in contr.Components)
        {
            if (((MetroStyleManager)item).Owner == owner)
            {
                manager = (MetroStyleManager)item;
            }
        }
        return manager;
    }
}

Step 2: In all your forms you will need to call the inferior method in the "Load" method. Excemple Form1.cs for

private void Form1_Load(object sender, EventArgs e)
{
    this.components.SetStyle(this);
}