更改组框文本颜色?

发布于 2024-07-23 05:11:48 字数 74 浏览 8 评论 0原文

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

谢谢! 艾伦

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独孤求败 2024-07-30 05:11:48

使用前景色< /a> 属性。 示例代码:

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

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);
    }
}
岁月静好 2024-07-30 05:11:48

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

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

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

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.

握住我的手 2024-07-30 05:11:48

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

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

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:

        foreach (Control c in this.groupBox1.Controls)
        {
            c.ForeColor = this.groupBox1.ForeColor; //or whatever color you want
        }
两仪 2024-07-30 05:11:48

或者我稍微更改了您的代码,以便用户可以仅在 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 ForeColor - 而所有其他控件前景色将保持默认值(黑色的)。

我的一分钱。

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

    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).

a cent of mine.

凯凯我们等你回来 2024-07-30 05:11:48

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文