[ACCEPTED]-(C#) How to make a dark mode theme in windows forms (separate form as select theme menu)-winforms

Accepted answer
Score: 14
  • 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.

0

More Related questions