Skip to content

Fix GroupBox doesn't apply ForeColor in ambient condition#14638

Open
Sathish-087 wants to merge 3 commits into
dotnet:mainfrom
Sathish-087:Fix_Issue_9958
Open

Fix GroupBox doesn't apply ForeColor in ambient condition#14638
Sathish-087 wants to merge 3 commits into
dotnet:mainfrom
Sathish-087:Fix_Issue_9958

Conversation

@Sathish-087

@Sathish-087 Sathish-087 commented Jun 11, 2026

Copy link
Copy Markdown

Fixes #9958

Proposed changes

  • Fixed GroupBox not respecting ambient ForeColor when its own ForeColor is not explicitly set.
  • Updated rendering logic to fall back to the parent Form.ForeColor instead of defaulting to Visual Styles text color.
  • Ensured consistency with other controls like Button and Label, which already respect ambient ForeColor.

Customer Impact

  • Applications that rely on setting ForeColor at the Form level will now see consistent text color in GroupBox.
  • Improves UI consistency and expected behavior across controls without requiring additional manual overrides.

Regression?

  • No

Risk

  • Low
    • Change is limited to GroupBox rendering logic.
    • Only affects scenarios where ForeColor is not explicitly set on GroupBox.
    • Explicit ForeColor settings on GroupBox continue to take precedence.

Screenshots

Before

image

After

image

Test methodology

  • Verified behavior with default GroupBox.ForeColor and custom Form.ForeColor.
  • Tested explicit override:
    • When GroupBox.ForeColor is set, it takes precedence.
  • Dynamically changed GroupBox.ForeColor at runtime and verified correct updates.
  • Compared behavior with Button and Label to ensure consistency.
  • Manually validated rendering with Visual Styles enabled.

Accessibility testing

NA

Test environment(s)

  • OS: Windows
  • SDK: 11.0.100-preview.3.26170.106
Microsoft Reviewers: Open in CodeFlow

Added logic to determine foreground color based on the form's ForeColor property.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to fix a long-standing WinForms behavior issue where GroupBox does not respect ambient/inherited ForeColor when Visual Styles are enabled, causing the caption text color to fall back to the theme’s default rather than the parent’s ForeColor.

Changes:

  • Updates GroupBox.OnPaint (Visual Styles path) to pass a text color instead of always deferring to the theme when ForeColor isn’t explicitly set.
  • Introduces a fallback to Form.ForeColor when it differs from SystemColors.ControlText.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +443 to +463
if (FindForm() is Form form && form.ForeColor != SystemColors.ControlText)
{
GroupBoxRenderer.DrawGroupBox(
e,
new Rectangle(0, 0, Width, Height),
Text,
Font,
form.ForeColor,
textFlags,
gbState);
}
else
{
GroupBoxRenderer.DrawGroupBox(
e,
new Rectangle(0, 0, Width, Height),
Text,
Font,
textFlags,
gbState);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced FindForm() with this.ForeColor, which already walks the full parent chain via WinForms ambient property inheritance — so intermediate parents with a custom ForeColor are correctly respected.

else
{
GroupBoxRenderer.DrawGroupBox(
if (FindForm() is Form form && form.ForeColor != SystemColors.ControlText)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added three unit tests covering: ambient ForeColor inherited from a Form, ambient ForeColor inherited from an intermediate Panel (the exact scenario FindForm() would have missed), and explicit ForeColor taking precedence over any ambient value.

else
{
GroupBoxRenderer.DrawGroupBox(
if (FindForm() is Form form && form.ForeColor != SystemColors.ControlText)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The fix uses FindForm() which jumps directly to the top-level Form. If the GroupBox is inside a Panel or UserControl that has a custom ForeColor, the ambient color from that intermediate parent is ignored.

    WinForms ambient property inheritance walks the full parent chain. this.ForeColor already returns the correct inherited value from any ancestor. A simpler and more correct fix would be to check whether the inherited ForeColor differs from the default

  2. form.ForeColor != SystemColors.ControlText assumes the default form ForeColor is always SystemColors.ControlText. If a high-contrast theme or custom Windows theme changes the default, this comparison could produce incorrect results

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! Both points have been addressed:

  • Replaced FindForm() with this.ForeColor, which already walks the full parent chain via WinForms ambient property inheritance — so intermediate parents with a custom ForeColor are correctly respected.
  • Replaced the form.ForeColor != SystemColors.ControlText comparison with ForeColor != DefaultForeColor. DefaultForeColor is theme-aware and resolves correctly under high-contrast and custom Windows themes.

Comment thread src/System.Windows.Forms/System/Windows/Forms/Controls/GroupBox/GroupBox.cs Outdated
@Sathish-087

Copy link
Copy Markdown
Author

@dotnet-policy-service agree company="Syncfusion, Inc."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GroupBox doesn't apply ForeColor in ambient condition

3 participants