I want to change my (form,buttons & labels) colors by using class file i don't know how to change them by using class file?

119 Views Asked by At

I have a mainform and i want to change the colour through the class file which is an separate file.but when i try to implent that i am facing errors please help me with this.

public void Themecolor()
{
    this.BackColor = Color.Black;
    this.ForeColor = Color.White;
}

1

There are 1 best solutions below

0
SH7 On

One way of handling the Theme is as follow.

Step 1: You can have a separate class file containing the Themes Colors you need to change.

public static class CustomColorTheme
{
    public static string BackColor = "#000000";
    public static string ForeColor = "#FFFFFF";
}

Step 2: On Form Initialize or Load you can get all Controls in the Form as set its colors accordingly from the Theme class

 public Form1()
 {
      InitializeComponent();

      //Set Form Back Color and Fore Color
      this.BackColor = ColorTranslator.FromHtml(CustomColorTheme.BackColor);
      this.ForeColor = ColorTranslator.FromHtml(CustomColorTheme.ForeColor);

      //Get all Controls in the Form
      foreach (Control c in this.Controls)
      {
         UpdateColorControls(c);
      }
    }

    //Set Theme color for all Controls in the Form
    public void UpdateColorControls(Control myControl)
    {
        myControl.BackColor = ColorTranslator.FromHtml(CustomColorTheme.BackColor);
        myControl.ForeColor = ColorTranslator.FromHtml(CustomColorTheme.ForeColor);
        foreach (Control subC in myControl.Controls)
        {
            UpdateColorControls(subC);
        }
    }

The Output:

Output