[ACCEPTED]-Change group box text color?-textcolor

Accepted answer
Score: 14

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);
    }
}

0

Score: 6

Actually all the answers posted here changes 11 the forecolor of other controls like button, label 10 etc residing inside the groupbox. To specifically 9 change just the text colour of the groupbox 8 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 7 above code can be meaningless if you are 6 adding controls programmatically later to 5 the groupbox, but the good thing is you 4 can handle all that situations by adding 3 extra conditions in code. To be doubly sure, a 2 list of keyvaluepair of control and forecolor 1 can be employed.

Score: 4

If you're referring to the groupbox text 3 itself, then use what Jon Skeet posted. If 2 you're referring to all the subsequent controls 1 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
        }
Score: 2

Or I have changed your code a bit so user 6 can choose between 2 types of color for 5 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 4 to the upper mehod, will change the groupBox 3 ForeColor only - while all other controls 2 forecolor will remain default (black).

a 1 cent of mine.

Score: 1

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

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

More Related questions