tags:

views:

377

answers:

1

I need to change the group box text to a specific color without changing the color of what is inside the group box.

The following code sets the ForeColor of the GroupBox to pink but this settings cascades to all the child controls as well:

groupbox.ForeColor = Color.Pink

How do I change the ForeColor of a GroupBox without having that color applied to every child control as well?

+3  A: 

You could iterate through all the controls in the GroupBox and set their respective ForeColor properties:

groupBox1.ForeColor = Color.Pink;
foreach (Control ctl in groupBox1.Controls) {
    ctl.ForeColor = SystemColors.ControlText;
}
Patrick McDonald
-1 This needs to be recursive to *all* child controls.
Andrew Hare
@Andrew, no it doesn't, any children of child controls which are themselves containers will "inherit" the ForeColor property from their immediate parent, not from the GroupBox
Patrick McDonald
Ah, yes you are right! (-1) removed and my answer deleted.
Andrew Hare