5

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

user3262364
  • 369
  • 3
  • 9
  • 23

5 Answers5

6

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);
   } 
}
User999999
  • 2,500
  • 7
  • 37
  • 63
3

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;
}
Batu.Khan
  • 3,060
  • 2
  • 19
  • 26
0

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.

W0lfw00ds
  • 2,018
  • 14
  • 23
0
 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

  • Hello and welcome to SO! While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Tomer Shetah Dec 25 '20 at 08:36
0

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;