Holzkopf Bude
Holzkopf Bude

Reputation: 193

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

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.

Upvotes: 18

Views: 37903

Answers (2)

BlueMystic
BlueMystic

Reputation: 2297

I actually made a Class "DarkModeCS" to automatically Apply DarkMode Color into all your Controls.

Check it: https://github.com/BlueMystical/Dark-Mode-Forms

Upvotes: 3

WENDYN
WENDYN

Reputation: 740

  • 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.

Upvotes: 21

Related Questions