From 6b3f581849419c42a1aa59df540f38b2fd4b37ea Mon Sep 17 00:00:00 2001 From: "Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)" Date: Thu, 26 Mar 2026 20:27:18 -0300 Subject: [PATCH 1/2] Fixes #14420 `ResetForeColor()` sets the default `ForeColor` to `Color.White`. `ShouldSerializeForeColor()` returns `false` when `ForeColor` equals `Color.White`, as it is indistinguishable from the default. In `DrawMessage`, the fix introduced by #13863 used `ShouldSerializeForeColor() ? ForeColor : SystemColors.ControlText`, which causes any explicitly set White `ForeColor` to fall back to `SystemColors.ControlText` (Black), since it matches the default value. - In `DrawMessage`, replace `ShouldSerializeForeColor() ? ForeColor : SystemColors.ControlText` with `ForeColor` directly. Since the default is already `Color.White` (set by `ResetForeColor`), this is correct for all cases: default, explicitly set White, and any other custom color. `PrintPreviewControl` with `ForeColor` set to `White` renders the message text as Black instead of White, both in the designer and at runtime. - Yes, introduced in #13863. - Minimal. Single-line change in `DrawMessage`. High Contrast override is unaffected. - Manual, using the ScratchProject with a `PrintPreviewControl` set to `ForeColor = Color.White`. - Unit tests added for default ForeColor value, `ShouldSerializeForeColor` behavior with default and non-default values. - 11.0.100-preview.3.26170.106 --- .../Forms/Printing/PrintPreviewControl.cs | 2 +- .../Printing/PrintPreviewControlTests.cs | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs b/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs index cd5315b5a44..769efd31976 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs @@ -592,7 +592,7 @@ private void CalculatePageInfo() private void DrawMessage(Graphics g, Rectangle rect, bool isExceptionPrinting) { - Color brushColor = ShouldSerializeForeColor() ? ForeColor : SystemColors.ControlText; + Color brushColor = ForeColor; if (SystemInformation.HighContrast && Parent is Control parent) { brushColor = parent.BackColor; diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs index ab5c1f19a24..8d85ea9daea 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs @@ -24,6 +24,32 @@ public void ShowPrintPreviewControl_BackColorIsCorrect() Assert.Equal(Color.Green.ToArgb(), actualBackColorArgb); } + [Fact] + public void PrintPreviewControl_ForeColor_DefaultIsWhite() + { + PrintPreviewControl control = new(); + + Assert.Equal(Color.White.ToArgb(), control.ForeColor.ToArgb()); + } + + [Fact] + public void PrintPreviewControl_ForeColorWhite_ShouldSerializeReturnsFalse() + { + PrintPreviewControl control = new(); + + // White is the default, so ShouldSerializeForeColor should return false. + Assert.False(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); + } + + [Fact] + public void PrintPreviewControl_ForeColorNonDefault_ShouldSerializeReturnsTrue() + { + PrintPreviewControl control = new(); + control.ForeColor = Color.Red; + + Assert.True(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); + } + [Fact] public void ShowPrintPreviewControlHighContrast_BackColorIsCorrect() { From ae86f32e72c847513a871ca5fd962ec24c9d4c3e Mon Sep 17 00:00:00 2001 From: "Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)" Date: Tue, 31 Mar 2026 20:08:55 -0300 Subject: [PATCH 2/2] Handles Feedbacks --- .../Forms/Printing/PrintPreviewControl.cs | 12 ++++++-- .../Printing/PrintPreviewControlTests.cs | 30 +++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs b/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs index 769efd31976..342ff10af83 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs @@ -49,6 +49,7 @@ public partial class PrintPreviewControl : Control private double _zoom = DefaultZoom; private bool _pageInfoCalcPending; private bool _exceptionPrinting; + private bool _isForeColorSet; /// /// Initializes a new instance of the class. @@ -57,6 +58,7 @@ public PrintPreviewControl() { ResetBackColor(); ResetForeColor(); + ForeColorChanged += (_, _) => _isForeColorSet = true; Size = new Size(100, 100); SetStyle(ControlStyles.ResizeRedraw, false); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true); @@ -295,9 +297,13 @@ public override string Text internal override bool ShouldSerializeBackColor() => !BackColor.Equals(SystemColors.AppWorkspace); [EditorBrowsable(EditorBrowsableState.Never)] - public override void ResetForeColor() => ForeColor = Color.White; + public override void ResetForeColor() + { + ForeColor = SystemColors.ControlText; + _isForeColorSet = false; + } - internal override bool ShouldSerializeForeColor() => !ForeColor.Equals(Color.White); + internal override bool ShouldSerializeForeColor() => _isForeColorSet; internal override bool SupportsUiaProviders => true; @@ -592,7 +598,7 @@ private void CalculatePageInfo() private void DrawMessage(Graphics g, Rectangle rect, bool isExceptionPrinting) { - Color brushColor = ForeColor; + Color brushColor = _isForeColorSet ? ForeColor : SystemColors.ControlText; if (SystemInformation.HighContrast && Parent is Control parent) { brushColor = parent.BackColor; diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs index 8d85ea9daea..eaaf19a51fc 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs @@ -25,31 +25,49 @@ public void ShowPrintPreviewControl_BackColorIsCorrect() } [Fact] - public void PrintPreviewControl_ForeColor_DefaultIsWhite() + public void PrintPreviewControl_ForeColor_DefaultIsControlText() { PrintPreviewControl control = new(); - Assert.Equal(Color.White.ToArgb(), control.ForeColor.ToArgb()); + Assert.Equal(SystemColors.ControlText.ToArgb(), control.ForeColor.ToArgb()); } [Fact] - public void PrintPreviewControl_ForeColorWhite_ShouldSerializeReturnsFalse() + public void PrintPreviewControl_ForeColorNotSet_ShouldSerializeReturnsFalse() { PrintPreviewControl control = new(); - // White is the default, so ShouldSerializeForeColor should return false. + // ForeColor not explicitly set: ShouldSerializeForeColor must return false. Assert.False(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); } + [Fact] + public void PrintPreviewControl_ForeColorWhiteExplicitlySet_ShouldSerializeReturnsTrue() + { + // Regression: #14420 - explicitly setting White must be distinguishable from the default. + PrintPreviewControl control = new() { ForeColor = Color.White }; + + Assert.True(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); + } + [Fact] public void PrintPreviewControl_ForeColorNonDefault_ShouldSerializeReturnsTrue() { - PrintPreviewControl control = new(); - control.ForeColor = Color.Red; + PrintPreviewControl control = new() { ForeColor = Color.Red }; Assert.True(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); } + [Fact] + public void PrintPreviewControl_ForeColorReset_ShouldSerializeReturnsFalse() + { + PrintPreviewControl control = new() { ForeColor = Color.Red }; + control.ResetForeColor(); + + Assert.False(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); + Assert.Equal(SystemColors.ControlText.ToArgb(), control.ForeColor.ToArgb()); + } + [Fact] public void ShowPrintPreviewControlHighContrast_BackColorIsCorrect() {