Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change group box text color?

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

like image 806
user20493 Avatar asked Jun 02 '09 19:06

user20493


People also ask

How do you change the color of the text in a group box?

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

How do you change color text?

Select the text that you want to change. On the Home tab, in the Font group, choose the arrow next to Font Color, and then select a color.


2 Answers

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);
    }
}
like image 94
Jon Skeet Avatar answered Sep 24 '22 14:09

Jon Skeet


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.

like image 36
nawfal Avatar answered Sep 26 '22 14:09

nawfal