Question

On my system, the caption of a groupbox is always a dark blue colour, how do I change this?

The answer to How do you change the color of the border on a group box? shows how I can override the drawing of the caption and border, but I don't want to have to deal with Visual Styles and drawing rounded corners and the like.

Was it helpful?

Solution

ForeColor is the property that controls the color of the text in a groupbox.

OTHER TIPS

This ought to do the trick:

public Form1()
{
  InitializeComponent();
  GroupBoxRenderer.RenderMatchingApplicationState = false;
  groupBox1.ForeColor = Color.Green;
}

It seems I can set the caption colour by setting the ForeColor to the colour I want and setting the FlatStyle to Standard.

If the FlatStyle is System, or if it's Standard and the ForeColor isn't changed from the default, then the caption color is set to the color specified in the XP Theme.

The above did not help me.
I found the solution here by adding the GroupBox.Header tag:

<GroupBox>
  <GroupBox.Header>
      <TextBlock Text="Header" Foreground="Black"/>
  </GroupBox.Header>
</GroupBox>

In Delphi at least, the caption is just the font color, you want to make sure parentfont is false. But that may not be useful at all to you since you tagged your question .net

Setting

groupBox1.ForeColor

changes the forecolor of other controls like button, label etc residing inside the groupbox which has to be unwelcome in most cases if you only need to change the text colour of groupbox. A simple workaround will be

    private void button1_Click(object sender, EventArgs e)
    {
        List<Color> lstColour = new List<Color>();
        foreach (Control c in groupBox1.Controls)
            lstColour.Add(c.ForeColor);

        groupBox1.ForeColor = Color.Red; //the colour you prefer for the text

        int index = 0;
        foreach (Control c in groupBox1.Controls)
        {
            c.ForeColor = lstColour[index];
            index++;
        }
    }

Of course the above code can be meaningless if you are adding controls programmatically later to the groupbox, but the good thing is you can handle all that situations by adding extra conditions in code. To be doubly sure, a list of keyvaluepair of control and forecolor can be employed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top