How to set the font color of a label in the same way as the color of a GroupBox subtitle?

I want to have several labels in the form with the same font color as the heading in my group mailboxes, and in addition, I want these colors to change if the user applies a different theme on their system.

Can I do this without changing the default GroupBox header?

UPDATE:

I tried setting Label ForeColor to ActiveCaption, it looks fine for the default scheme (blue), but when I change the scheme to Olive Green, the labels of the labels and group fields do not match.

In addition, the usual behavior of a GroupBox is that setting FlatStyle to Standard sets the ForeColor header color, however, to create a new GroupBox and set ForeColor to ControlText you must first set it to something other than ControlText and then set it back. (If you do not follow what I mean, try it and see.)

+4
source share
4 answers

Hm, the same question? I will 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; } } 
+10
source

In appearance, the GroupBox draws its Caption using SystemColors.ActiveCaption, but you will need to check this with other themes.

Strange it is not reflected in the ForeColor property, but if you set this property, then it takes over.

So the answer is:

 private void Form1_Load(object sender, EventArgs e) { label1.ForeColor = SystemColors.ActiveCaption; } 
+1
source

The ForeColorChanged event is fired on the label. Then you can do something like this:

 this.label1.ForeColorChanged += (o,e) => { this.groupBox1.ForeColor = this.label1.ForeColor;}; 

If you are trying to detect when a user changes the subject, you can connect to SystemEvents, which can be found in the Microsoft.Win32 namespace. Something like that:

  Microsoft.Win32.SystemEvents.UserPreferenceChanged += new Microsoft.Win32.UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged); void SystemEvents_UserPreferenceChanged(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e) { this.groupBox1.ForeColor = this.label1.ForeColor; } 
0
source

I assume that you are using Windows Forms and not WPF. When you apply colors, use the colors of the system (for example, Control or HighlightText), they will change when the user switches the theme of Windows. Here is the code to set the group field color to the system color, and then apply this color to the label:

 groupBox1.ForeColor = SystemColors.ActiveBorder; label1.ForeColor = groupBox1.ForeColor; 
0
source

All Articles