Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change color of all controls inside the form in c#

Tags:

c#

winforms

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

like image 438
user3262364 Avatar asked Apr 08 '14 11:04

user3262364


People also ask

How do I change the background color of a form?

Change the color, theme, or header imageIn Google Forms, open a form. Under "Color," you can choose a theme color and background color for your form.

How do I change the color of my winform title bar?

Right-click on the desktop, and select "Personalize". Click on the "Window Color" tile at the bottom of the screen. Choose your new color. If your computer is configured to use the Aero theme, you can choose from one of the standard colors or mix one of your own.

What is ForeColor C#?

The ForeColor property is an ambient property. An ambient property is a control property that, if not set, is retrieved from the parent control. For example, a Button will have the same BackColor as its parent Form by default.

How do I add different controls to my form?

Add the control by drawingSelect the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.


2 Answers

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);
   } 
}
like image 131
User999999 Avatar answered Oct 22 '22 20:10

User999999


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;
}
like image 3
Batu.Khan Avatar answered Oct 22 '22 20:10

Batu.Khan