How To Use Metro Framework Metro Style Manager in C#

By FoxLearn 12/9/2024 2:10:55 PM   17.1K
The MetroFramework is a popular open-source library that provides a modern Metro UI for Windows Forms applications in C#.

The MetroStyleManager is part of this framework and allows you to manage themes and styles dynamically.

In this article, we'll explore how to dynamically change the theme and color of a MetroFramework-based Windows Forms application using the MetroStyleManager.

How To Use Metro Framework Metro Style Manager in C#?

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "MetroStyle" and then click OK

You can install the library using NuGet Package Manager by right-clicking on your project select Manage NuGet Packages -> Search metro framework -> Install

install metro framework

Design metro form as shown below.

c# metro ui

Each Metro control has a StyleManager property. Set this property to the MetroStyleManager instance you added to your form.

You can modify the theme (Light or Dark) and the color style dynamically using the MetroStyleManager.

Add code to handle your metro form as shown below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MetroStyle
{
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        public Form1()
        {
            InitializeComponent();
            // Initialize the MetroStyleManager
            this.StyleManager = metroStyleManager1;
        }

        private void mbTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Select metro theme
            switch (mbTheme.SelectedIndex)
            {
                case 0:
                    metroStyleManager1.Theme = MetroFramework.MetroThemeStyle.Dark;
                    break;
                case 1:
                    metroStyleManager1.Theme = MetroFramework.MetroThemeStyle.Light;
                    break;
            }
        }

        private void mbColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Select metro color
            metroStyleManager1.Style = (MetroFramework.MetroColorStyle)Convert.ToInt32(mbColor.SelectedIndex);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mbColor.SelectedIndex = 0;
            mbTheme.SelectedIndex = 0;
        }
    }
}

This example illustrates how you can dynamically control UI themes and styles using the MetroFramework's MetroStyleManager. Users can switch between Light/Dark themes and various color styles seamlessly.

VIDEO TUTORIAL