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...
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.
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.
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.
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.
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);
}
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With