Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(C#) How to make a dark mode theme in windows forms (separate form as select theme menu)

Tags:

c#

winforms

I want to know how I can code a dark theme radio button that turns my entire C# windows form dark (including menus etc)

I made a separate settings form and I want to have radio buttons for themes that change the theme in the whole program, not just the settings menu. I'm making a text editor.

like image 284
Holzkopf Bude Avatar asked Apr 10 '20 17:04

Holzkopf Bude


1 Answers

  • Step 1: Decide on how you want to store your color schemes and create a class based on it.
  • Step 2: Create a method that changes the color of every UI component within the container, like this:
public void ChangeTheme(ColorScheme scheme, Control.ControlCollection container)
{
    foreach (Control component in container)
    {
        if (component is Panel)
        {
            ChangeTheme(scheme, component.Controls);
            component.BackColor = scheme.PanelBG;
            component.ForeColor = scheme.PanelFG;
        }
        else if (component is Button)
        {
            component.BackColor = scheme.ButtonBG;
            component.ForeColor = scheme.ButtonFG;
        }
        else if (component is TextBox)
        {
            component.BackColor = scheme.TextBoxBG;
            component.ForeColor = scheme.TextBoxFG;
        }
        ...
    }
}
  • Step 3: Call that method whenever you open new form on its components (and also make sure the form isn't visible before the method is done for obvious reasons) or whenever you change the theme.
like image 51
WENDYN Avatar answered Sep 21 '22 22:09

WENDYN