Fix GroupBox doesn't apply ForeColor in ambient condition#14638
Fix GroupBox doesn't apply ForeColor in ambient condition#14638Sathish-087 wants to merge 3 commits into
Conversation
Added logic to determine foreground color based on the form's ForeColor property.
There was a problem hiding this comment.
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 whenForeColorisn’t explicitly set. - Introduces a fallback to
Form.ForeColorwhen it differs fromSystemColors.ControlText.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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); | ||
| } |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
-
The fix uses
FindForm()which jumps directly to the top-levelForm. If theGroupBoxis inside aPanelorUserControlthat has a customForeColor, the ambient color from that intermediate parent is ignored.WinForms ambient property inheritance walks the full parent chain.
this.ForeColoralready 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 -
form.ForeColor != SystemColors.ControlTextassumes the default form ForeColor is alwaysSystemColors.ControlText. If a high-contrast theme or custom Windows theme changes the default, this comparison could produce incorrect results
There was a problem hiding this comment.
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.
|
@dotnet-policy-service agree company="Syncfusion, Inc." |
Fixes #9958
Proposed changes
Customer Impact
Regression?
Risk
Screenshots
Before
After
Test methodology
Accessibility testing
NA
Test environment(s)
Microsoft Reviewers: Open in CodeFlow