Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the Font color of a label to the same as the caption color of a GroupBox?

I want to have some labels on a form with the same font color as the caption on my group boxes, and furthermore I want these colors to change if the user has applied a different Theme on their system.

Can I do this without changing the GroupBox caption from its default?

UPDATE:

I have tried setting the Label ForeColor to ActiveCaption, this looks okay for the Default (Blue) scheme, but when I change the scheme to Olive Green, the label and group box captions are not the same.

Also, the GroupBox normal behaviour is that setting the FlatStyle to Standard sets the caption colour to ForeColor, however to create a new GroupBox and set its ForeColor to ControlText, you must first set it to something other than ControlText and then set it back again. (If you don't follow what I mean, then try it and see.)

like image 918
Patrick McDonald Avatar asked Dec 30 '22 04:12

Patrick McDonald


1 Answers

Hmm, same question? I'll repeat my post:

using System.Windows.Forms.VisualStyles;
...

    public Form1()
    {
      InitializeComponent();
      if (Application.RenderWithVisualStyles)
      {
        VisualStyleRenderer rndr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
        Color c = rndr.GetColor(ColorProperty.TextColor);
        label1.ForeColor = c;
      }
    }
like image 157
Hans Passant Avatar answered Jan 18 '23 05:01

Hans Passant