List of Exercises

Create Color Theme for Winforms Controls in c#

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Color_Thems
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      Change Theme index between 0 to 26
      changeTheme(0);
    }
    private void changeTheme(int i)
    {
      var color = ColorTranslator.FromHtml(ThemeColor.ColorList[i]);
      ThemeColor.PrimaryColor = color;
      ThemeColor.SecondaryColor = ThemeColor.ChangeColorBrightness(color, -0.5);
      ThemeColor.ThirdColor = ThemeColor.ChangeColorBrightness(color, 0.5);
foreach (Control ctrl in this.Controls)
      {
        if (ctrl.GetType() == typeof(Button))
        {
          Button btn = (Button)ctrl;
          btn.BackColor = ThemeColor.PrimaryColor;
          btn.ForeColor = Color.White;
          btn.FlatAppearance.BorderColor = ThemeColor.SecondaryColor;
        }
        if (ctrl.GetType() == typeof(Label))
        {
          Label btn = (Label)ctrl;
          btn.ForeColor = ThemeColor.PrimaryColor;
        }
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox btn = (CheckBox)ctrl;
          btn.ForeColor = ThemeColor.PrimaryColor;
        }
        if (ctrl.GetType() == typeof(ComboBox))
        {
          ComboBox btn = (ComboBox)ctrl;
          btn.BackColor = ThemeColor.ThirdColor;

        }
      }
    }
    private void button2_Click(object sender, EventArgs e)
    {
      changeTheme(Convert.ToInt16(textBox1.Text) - 1);
      textBox1.Text = (Convert.ToInt16(textBox1.Text) + 1).ToString();
    }

  }
}
//===============================ThemeColor.cs==================================
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public static class ThemeColor
{

  public static Color PrimaryColor { get; set; }
  public static Color SecondaryColor { get; set; }
  public static Color ThirdColor { get; set; }
  public static List<string> ColorList = new List<string>() { "#3F51B5",
                                  "#009688",
                                  "#FF5722",
                                  "#607D8B",
                                  "#FF9800",
                                  "#9C27B0",
                                  "#2196F3",
                                  "#EA676C",
                                  "#E41A4A",
                                  "#5978BB",
                                  "#018790",
                                  "#0E3441",
                                  "#00B0AD",
                                  "#721D47",
                                  "#EA4833",
                                  "#EF937E",
                                  "#F37521",
                                  "#A12059",
                                  "#126881",
                                  "#8BC240",
                                  "#364D5B",
                                  "#C7DC5B",
                                  "#0094BC",
                                  "#E4126B",
                                  "#43B76E",
                                  "#7BCFE9",
                                  "#B71C46"};
  public static Color ChangeColorBrightness(Color color, double correctionFactor)
  {
    double red = color.R;
    double green = color.G;
    double blue = color.B;

    //If correction factor is less than 0, darken color.
    if (correctionFactor < 0)
    {
      correctionFactor = 1 + correctionFactor;
      red *= correctionFactor;
      green *= correctionFactor;
      blue *= correctionFactor;
    }
    //If correction factor is greater than zero, lighten color.
    else
    {
      red = (255 - red) * correctionFactor + red;
      green = (255 - green) * correctionFactor + green;
      blue = (255 - blue) * correctionFactor + blue;
    }

    return Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
  }

}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


Find Out How to Create the Perfect Color Scheme for Your Winforms Controls!


Video Thumbnail