C# 更改组框文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/941492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:51:20  来源:igfitidea点击:

Change group box text color?

c#groupboxtextcolor

提问by user20493

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.

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

Thanks! Alan

谢谢!艾伦

采纳答案by Jon Skeet

Use the ForeColorproperty. Sample code:

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

回答by Peter

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

我假设您现在使用的是 winforms 而不是 WPF。

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

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

回答by BFree

If you're referring to the groupbox text itself, then use what Jon Skeet posted. If you're referring to all the subsequent controls in the groupbox, then you can use this code:

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

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

回答by nawfal

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.

实际上,此处发布的所有答案都会更改位于 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++;
        }
    }

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.

当然,如果您稍后以编程方式向 groupbox 添加控件,则上述代码可能毫无意义,但好处是您可以通过在代码中添加额外条件来处理所有这些情况。可以肯定的是,可以使用控件和前景色的键值对列表。

回答by Mitja Bonca

Or I have changed your code a bit so user can choose between 2 types of color for groupBox only:

或者我对您的代码进行了一些更改,以便用户只能在 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;
        }
    }

Passing "true" or "false" values to the upper mehod, will change the groupBox ForeColor only - while all other controls forecolor will remain default (black).

将“true”或“false”值传递给上层方法,只会更改 groupBox ForeColor - 而所有其他控件的前景色将保持默认(黑色)。

a cent of mine.

我的一分钱。