Change group box text color?
Asked Answered
B

5

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

Bestiality answered 2/6, 2009 at 19:37 Comment(1)
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.Madalynmadam
G
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);
    }
}
Galbraith answered 2/6, 2009 at 19:40 Comment(0)
C
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.

Capture answered 21/9, 2011 at 2:25 Comment(3)
Great a work around mate! This is what I was looking for. Votes +1 :)Anemophilous
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.Asymptote
@DeniseSkidmore yes good point, I mentioned it already in the answer along that line.Capture
T
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
        }
Teishateixeira answered 2/6, 2009 at 19:45 Comment(0)
A
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.

Anemophilous answered 23/5, 2012 at 5:12 Comment(0)
D
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.

Dimaggio answered 2/6, 2009 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.