Need Help Only Need Help Only - 5 months ago 19
Vb.net Question

How can I change the color of the group box text without changing its children forecolor?

I want to know if there is an option to just change the color of group box text at the top left of the group box in a windows form and not any controls or labels located inside of the group box.

I know that

GroupBox.ForeColor = Color.Blue
will change all text associated with that box to blue, but it also changes the
ForeColor
of labels and other controls in the
GroupBox
.

How can I change the color of the group box text without changing its children forecolor?

Answer

As long as I know, all the child controls will take the property of the parent.

You can store all your child colors and change them after you set the GroupBox's ForeColor. You can use a Dictionary with each pair of Control/Color.

Something like:

Dim cColors As New Dictionary(Of Control, Color)

For Each ctrl As Control In GroupBox1.Controls
    cColors.Add(ctrl, ctrl.ForeColor)
Next

GroupBox1.ForeColor = Color.Blue

For Each ctrl As Control In GroupBox1.Controls
    If cColors.HasKey(ctrl) Then
        ctrl.ForeColor = cColors(ctrl)
    End If
Next

You can put that in a method.

More information about the property at MSDN.