题
如何改变在C#中的一组框的文本颜色? “文档”甚至没有提到这一点,谷歌搜索还没有止跌回升的答案。
谢谢! 艾伦
解决方案
使用 ForeColor
属性。示例代码:
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);
}
}
其他提示
其实所有的答案张贴在这里改变其他控件像按钮,标签等驻留在组框里面的前景色。为了具体仅更改组框中的文本颜色有一个简单的解决方法。
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++;
}
}
当然,上面的代码可以是毫无意义的,如果你是编程后来到分组框中添加控件,但好处是,你可以通过在代码中添加额外的条件,处理所有的情况。为了加倍肯定,控制和前景色的keyvaluepair的列表可以被使用。
如果你指的是组框文本本身,那么用什么乔恩斯基特公布。如果你指的是所有后续控件组框,那么你可以使用此代码:
foreach (Control c in this.groupBox1.Controls)
{
c.ForeColor = this.groupBox1.ForeColor; //or whatever color you want
}
或者我已经改变了你的代码了一点,所以用户可以在2种颜色之间的GROUPBOX只选择:
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;
}
}
传递“真”或“假”值到上部mehod,将仅改变组框中前景色 - 而所有其他控件前景色将保持默认(黑色)
。矿的百分之。
我假设你在的WinForms不是WPF了。
要改变使用的一组框的文本颜色前景色这改变了的标题文字的字体颜色。
不隶属于 StackOverflow