'change color of all controls inside the form in c#

in one button click event i want to change form color and all control color inside the form(textbox,label,gridview,combobox) ,,so i given code like this:

  foreach (Control c in MyForm.Controls) {
    c.BackColor = Colors.Black;
    c.ForeColor = Colors.White;
}

but this is only changing the the label and group box color.
not able to change form and grid view column heading .
group box heading color.
how i can change color all controls inside the form
any help is very appreciable...



Solution 1:[1]

You have to use a recursive function

www.dotnetperls.com/recursion

something along the lines of:

foreach (Control c in MyForm.Controls) 
{
   UpdateColorControls(c);
}


public void UpdateColorControls(Control myControl)
{
   myControl.BackColor = Colors.Black;
   myControl.ForeColor = Colors.White;
   foreach (Control subC in myControl.Controls) 
   {
       UpdateColorControls(subC);
   } 
}

Please not that not all controls have a property ForeColor and BackColor

Update

if you wan't for instance only the textboxes to change:

public void UpdateColorControls(Control myControl)
{
   if (myControl is TextBox)
   {
       myControl.BackColor = Colors.Black;
       myControl.ForeColor = Colors.White;
   }
   if (myControl is DataGridView)
   {
      DataGridView MyDgv = (DataGridView)myControl;
      MyDgv.ColumnHeadersDefaultCellStyle.BackColor = Colors.Black;
      MyDgv.ColumnHeadersDefaultCellStyle.ForeColor = Colors.White;
   }

   // Any other non-standard controls should be implemented here aswell...

   foreach (Control subC in myControl.Controls) 
   {
       UpdateColorControls(subC);
   } 
}

Solution 2:[2]

You can check control's type and do different things for specific controls. For example for datagridviews :

if (c.GetType().ToString().IndexOf("DataGridView") != -1)
{
  DataGridView dgv = (DataGridView)c;
  dgv.DefaultCellStyle.ForeColor = Color.Red;
}

Solution 3:[3]

There are different names for the background color property, depending on the control type. For example the labels background is behind a property named "BackColor", and datagrid uses a name "BackgroundColor".

You can use this code as base for your coloring. The DataGridView is handled as an "exception":

// Loop each control
foreach (Control control in this.Controls)
{
   if (control is DataGridView) { ((DataGridView)control).BackgroundColor = Color.Red; }

   // else if (control is ...) { ... }

   // Everything else is colored by using property "BackColor"
   else { control.BackColor = Color.Red; }
}

Add more "else if"s if needed. You need to use casting, as Visual Studio doesn't know what type the control is.

Solution 4:[4]

 var Hex_SET5_Red = DimGray;
            var Hex_SET5_Yellow = Black;
            var Hex_SET5_OrangeRed = Silver;
            var Hex_SET5_Maroon = Black;
            //Transfer String to Hex Colour
            var UseAble_Hex5_Red = System.Drawing.ColorTranslator.FromHtml(Hex_SET5_Red);
            var UseAble_Hex5_Yellow = System.Drawing.ColorTranslator.FromHtml(Hex_SET5_Yellow);
            var UseAble_Hex5_OrangeRed = System.Drawing.ColorTranslator.FromHtml(Hex_SET5_OrangeRed);
            var UseAble_Hex5_Maroon = System.Drawing.ColorTranslator.FromHtml(Hex_SET5_Maroon);
            //Filling the Panels with colors
            pnl17.BackColor = UseAble_Hex5_Red;
            pnl18.BackColor = UseAble_Hex5_Yellow;
            pnl19.BackColor = UseAble_Hex5_OrangeRed;
            pnl20.BackColor = UseAble_Hex5_Maroon;

// visit https://www.youtube.com/watch?v=MSLzDm1fXQo&ab_channel=NAASSOFT

Solution 5:[5]

For DataGridView : I could not succeed changing DataGridView Column header color until I change this EnableHeadersVisualStyles to false :

if (myControl is DataGridView)
            {
                DataGridView MyDgv = (DataGridView)myControl;
                MyDgv.EnableHeadersVisualStyles = false;
                MyDgv.ColumnHeadersDefaultCellStyle.BackColor = themebackcolor;
                MyDgv.ColumnHeadersDefaultCellStyle.ForeColor = themeforecolor; 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Batu.Khan
Solution 3 W0lfw00ds
Solution 4 Muzamil Ali
Solution 5 Dominique Mathieu