Search code examples
c#inheritancestylesfont-size

C# WinForms - How to prevent custom control from inherit the form font styling?


I've searched for days and nothing worked.

I'm working on a project using a custom style for WinForm called MaterialSkin, github page is https://github.com/IgnaceMaes/MaterialSkin

Basically to use it, I just change the inherited class from this public partial class FormMain : Form to this public partial class FormMain : MaterialForm and this applies the Material Design to my project.

The problem is that all controls are affected by this change. If I add a label, and change it font, the visual change only works in design mode, when I run the project, the MaterialSkin font is applied (Roboto).

Now I want to use a custom control called CircularProgressBar from this repo https://github.com/falahati/CircularProgressBar

And the problem is the same, I want a big number in center of the circle, and it works in design mode. When I run the project, the font size is reduced to 8,5.

I'm already tried to set the new font manually to the CircularProgressBar in Form_Load event and also in OnPaint, nothing work.

Any ideas about what I missing?

font size changed in runtime overwriting my definition

Edit

As suggested by @Jimi, I'm posting the minimal code below to reproduce the problem I'm facing.

  1. Created a brand new project Windows Forms App (.NET Framework) v4.6.1
  2. I've searched and installed MaterialSkin.2 v2.1.3 (not the latest) by leocb, IgnaceMaes
  3. I've searched and installed CircularProgressBar v2.8.0.16 by Soroush Falahati
  4. I've opened Form1.cs and made the necessary changes to load MaterialSkin
  5. I've added CircularProgressBarcontrol to Form1, Build and Run.
// Form1.cs
using MaterialSkin;
using MaterialSkin.Controls;

namespace SimpleTestProject
{
    public partial class Form1 : MaterialForm
    {
        private readonly MaterialSkinManager materialSkinManager = null;

        public Form1()
        {
            InitializeComponent();

            materialSkinManager = MaterialSkinManager.Instance;
            materialSkinManager.AddFormToManage(this);
            materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
        }
    }
}

The Result

Another running demo showing the problem (bug)


Solution

  • As suggested by @Jimi and @Leo, to avoid applying MaterialSkin style to all controls, just need to disable the MaterialSkinManager property called EnforceBackcolorOnAllComponents

    materialSkinManager.EnforceBackcolorOnAllComponents = false;