Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改组框文本颜色?_C#_Groupbox_Textcolor - Fatal编程技术网

C# 更改组框文本颜色?

C# 更改组框文本颜色?,c#,groupbox,textcolor,C#,Groupbox,Textcolor,如何在C#中更改组框的文本颜色?“文档”甚至没有提到这一点,谷歌也没有找到答案 谢谢! Alan使用该属性。示例代码: using System; using System.Drawing; using System.Windows.Forms; class Test { [STAThread] static void Main(string[] args) { Form form = new Form(); GroupBo

如何在C#中更改组框的文本颜色?“文档”甚至没有提到这一点,谷歌也没有找到答案

谢谢! Alan使用该属性。示例代码:

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

我假设你现在在winforms中,而不是WPF中


要更改分组框的文本颜色,请使用ForeColor,这会更改标题文本中的字体颜色。

如果您指的是分组框文本本身,请使用Jon Skeet发布的内容。如果要引用groupbox中的所有后续控件,则可以使用以下代码:

        foreach (Control c in this.groupBox1.Controls)
        {
            c.ForeColor = this.groupBox1.ForeColor; //or whatever color you want
        }

实际上,这里发布的所有答案都改变了groupbox中其他控件(如按钮、标签等)的前景色。要专门更改groupbox的文本颜色,有一个简单的解决方法

    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++;
        }
    }
private void按钮1\u单击(对象发送者,事件参数e)
{
List lstColour=新列表();
foreach(groupBox1.控件中的控件c)
lstColour.添加(c.前景色);
groupBox1.ForeColor=Color.Red;//文本的首选颜色
int指数=0;
foreach(groupBox1.控件中的控件c)
{
c、 前景色=LSTCLOR[索引];
索引++;
}
}

当然,如果以后以编程方式将控件添加到groupbox中,上述代码可能毫无意义,但好的一面是,您可以通过在代码中添加额外的条件来处理所有这些情况。可以确定的是,可以使用control和forecolor的keyvaluepair列表。

或者我已经稍微更改了您的代码,因此用户可以仅为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;
        }
    }
将“true”或“false”值传递给上方法,将仅更改groupBox前景色-而所有其他控件前景色将保持默认(黑色)


我的一分钱。

很有魅力!谢谢你,乔恩!在我的例子中,前景色设置正确(白色,可能是从表单继承的,前景色=白色,后景色=黑色),但显示蓝色-将其更改为另一种颜色并返回到属性窗口中就成功了!现在,属性以粗体显示,表明它已更改。这是一个很好的解决方法!这就是我要找的。投票+1:)注意,上面的不是线程安全的。如果您可能同时更改组框颜色和添加/删除控件,则可以使用字典或元组列表以颜色保存控件引用,然后在第二个循环中对该集合进行访问,而不是对可能已更改的控件列表进行访问。@Deniseskimore是的,很好,我在回答中已经提到了这一点。