6

How do you change the text color of a group box in C#? The "documentation" doesn't even mention this, and Googling hasn't turned up an answer.

Thanks! Alan

user20493
  • 5,704
  • 7
  • 34
  • 31
  • 6
    In my case, the ForeColor was set correctly (to White, probably inherited from the Form, which had ForeColor=White and BackColor=Black), but was showing Blue - changing it to another color and back in the properties window did the trick! Now, the property is displayed in bold, indicating that it was changed. – Daren Thomas Mar 11 '10 at 08:50

5 Answers5

13

Use the ForeColor property. Sample code:

using System;
using System.Drawing;
using System.Windows.Forms;

class Test
{       
    [STAThread]
    static void Main(string[] args)
    {
        Form form = new Form();
        GroupBox group = new GroupBox();
        group.Text = "Text";
        group.ForeColor = Color.Red;
        form.Controls.Add(group);
        Application.Run(form);
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

Actually all the answers posted here changes the forecolor of other controls like button, label etc residing inside the groupbox. To specifically change just the text colour of the groupbox there is a simple workaround.

    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.

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 1
    Great a work around mate! This is what I was looking for. Votes +1 :) – Mitja Bonca May 23 '12 at 05:04
  • Note the above is not thread safe. If you might be changing the group box color and adding/removing controls at the same time, you might use a dictionary or a list of tuples to save the control reference with the color, then foreach over that collection in the second loop instead of over the controls list that may have changed. – Denise Skidmore Aug 29 '13 at 16:23
  • @DeniseSkidmore yes good point, I mentioned it already in the answer along that line. – nawfal Aug 29 '13 at 17:15
4

If you're referring to the groupbox text itself, then use what Jon Skeet posted. If you're referring to all the subsequent controls in the groupbox, then you can use this code:

        foreach (Control c in this.groupBox1.Controls)
        {
            c.ForeColor = this.groupBox1.ForeColor; //or whatever color you want
        }
BFree
  • 102,548
  • 21
  • 159
  • 201
2

Or I have changed your code a bit so user can choose between 2 types of color for groupBox only:

    private void SettingGroupBoxColor(bool bSelected)
    {
        if (!bSelected)
            groupBox1.ForeColor = Color.Red;
        else
            groupBox1.ForeColor = Color.Green;
        foreach (Control c in this.groupBox1.Controls)
        {
            c.ForeColor = Color.Black;
        }
    }

Passing "true" or "false" values to the upper mehod, will change the groupBox ForeColor only - while all other controls forecolor will remain default (black).

a cent of mine.

Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
1

I'm assuming you are in winforms not in WPF now.

To change the text color of a group box you use ForeColor this changes the font colour in the header text.

Peter
  • 1,776
  • 13
  • 20