Monday, April 25, 2011

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

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?

From stackoverflow
  • 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;
    }
    
    Andrew Hare : -1 This needs to be recursive to *all* child controls.
    Patrick McDonald : @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
    Andrew Hare : Ah, yes you are right! (-1) removed and my answer deleted.

0 comments:

Post a Comment