2

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.

Sunil Acharya
  • 1,153
  • 5
  • 22
  • 39
  • If you want a spiffy Metro look; styling and colouring; you're arguably better off using _WPF_ and _MahApps Metro_. Much easier than WinForms. [That Github project](https://github.com/thielj/MetroFramework/graphs/contributors) you mentioned, looks pretty dead too sadly. No activity since mid 2013 –  Jan 20 '16 at 13:38
  • @Micky it's originally copy of MetroFramework and not for WCF. – Sunil Acharya Jan 20 '16 at 13:51
  • MetroFramework is fairly robust, I don't see a problem using a library 2 and a half years out of date if its robust! – Zach Ross-Clyne Jan 20 '16 at 15:11
  • @ZachRoss-Clyne Hello, I appreciate with your reply as you are using this framework so, I want to learn many thinks from you don't mind like the themes options and animation. currently, I want to change the themes style from MDI page or child form but with action, only the current form is changing the themes. My question is how to change entire application and controls themes according to MDI or any other form. – Sunil Acharya Jan 21 '16 at 14:28
  • @ZachRoss-Clyne if you don't mine can I have any contact of yours like twitter or email – Sunil Acharya Jan 21 '16 at 14:29
  • @Sunil Twitter ZRC2011 – Zach Ross-Clyne Jan 22 '16 at 00:29
  • You can look at this site. There is a lot of sample here using ModernMetroUI Design by DenrioDenise. http://denricdenise.info/2014/09/how-to-use-winforms-modern-ui/ – Shift 'n Tab Aug 07 '16 at 14:10
  • @ShiftN'Tab Yes I check but there is not enough information to learn more about MetroFramwork – Sunil Acharya Aug 10 '16 at 05:55
  • @SunilAcharya try this and download the sample application demo, there is a source to look up https://github.com/dennismagno/metroframework-modern-ui – Shift 'n Tab Aug 10 '16 at 08:02

3 Answers3

7

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);
}
Fatih GÜRDAL
  • 1,489
  • 1
  • 17
  • 19
  • Yes I did the project. @SunilAcharya [Project SS](http://imgur.com/a/q5g5X) – Fatih GÜRDAL Aug 11 '16 at 06:37
  • But how can I apply selected style and theme to all my opened forms? – Pratikk Apr 01 '17 at 08:36
  • Hi @Pratikk You must create a form which name is ""MyBaseWinForm" and all forms must be inherited from this. Add MetroStyleManager dynamically into the MyBaseWinForm's constructor method and you can put codes which are in "Example 2"' in this class. So that you can fix it via inheritance. – Fatih GÜRDAL Apr 01 '17 at 14:07
  • I am getting an error "Extension method must be defined in a non-generic static class". Can you send me an example of this, so that I can examine and implement it my project – Pratikk Apr 03 '17 at 07:43
  • @Pratikk The class in which extension methods are used must be separate and static. I added "Exemple 3" for you – Fatih GÜRDAL Apr 06 '17 at 11:04
1

I know this is an old question, but just like you were looking for a solution and it was as simple as using the Clone() method of the StyleManager, this will change the style to the form and all the MetroFrameWork controls that this contain

Form1 _form1 = new Form1();
this.StyleManager.Clone(_form1); //This will do everything for you

Note: The UseStyleColors property of the form controls must be enabledTrue, to work.

enter image description here

J. Rodríguez
  • 256
  • 1
  • 6
  • 21
0
metroStyleManager1.Theme = metroStyleManager1.Theme == MetroThemeStyle.Light ? MetroThemeStyle.Dark : MetroThemeStyle.Light;
this.Theme = metroStyleManager1.Theme;
this.Refresh();
jmattheis
  • 10,494
  • 11
  • 46
  • 58
Ramesh
  • 1
  • 1