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
To change the text color of a group box you use ForeColor this changes the font colour in the header 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.
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With