diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
index 751dacc7778..191f19c41e0 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
@@ -382,10 +382,7 @@ public ContentAlignment ImageAlign
}
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
if (value != _imageAlign)
{
@@ -671,10 +668,7 @@ public virtual ContentAlignment TextAlign
}
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
if (value == TextAlign)
{
diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/CheckBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/CheckBox.cs
index a48aeaa9386..81415db1974 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/CheckBox.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/CheckBox.cs
@@ -139,10 +139,7 @@ public ContentAlignment CheckAlign
}
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
if (_checkAlign != value)
{
diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Label.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Label.cs
index 1a03387aaa8..ad5c27ea768 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/Label.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/Label.cs
@@ -565,10 +565,7 @@ public ContentAlignment ImageAlign
}
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
if (value != ImageAlign)
{
@@ -724,10 +721,7 @@ public virtual ContentAlignment TextAlign
}
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
if (TextAlign != value)
{
diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/RadioButton.cs b/src/System.Windows.Forms/src/System/Windows/Forms/RadioButton.cs
index 7bb93b754b8..dc0a28b91c6 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/RadioButton.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/RadioButton.cs
@@ -156,10 +156,7 @@ public ContentAlignment CheckAlign
}
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
checkAlign = value;
if (OwnerDraw)
diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripControlHost.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripControlHost.cs
index bc4b47966ed..cf02e244187 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripControlHost.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripControlHost.cs
@@ -115,10 +115,7 @@ public ContentAlignment ControlAlign
get => _controlAlign;
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
if (_controlAlign != value)
{
diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs
index 1e5d83e9eeb..e090cf50c3a 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs
@@ -982,10 +982,7 @@ public ContentAlignment ImageAlign
get => _imageAlign;
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
if (_imageAlign != value)
{
@@ -1875,10 +1872,7 @@ public virtual ContentAlignment TextAlign
get => _textAlign;
set
{
- if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
- {
- throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
- }
+ SourceGenerated.EnumValidator.Validate(value);
if (_textAlign != value)
{
diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs b/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs
index 7fac5da17e4..12f38b53a98 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs
@@ -340,25 +340,6 @@ public static string GetComponentName(IComponent component, string defaultNameVa
public static class EnumValidator
{
- ///
- /// Valid values are 0x001,0x002,0x004, 0x010,0x020,0x040, 0x100, 0x200,0x400
- /// Method for verifying
- /// Verify that the number passed in has only one bit on
- /// Verify that the bit that is on is a valid bit by bitwise anding it to a mask.
- ///
- public static bool IsValidContentAlignment(ContentAlignment contentAlign)
- {
- if (BitOperations.PopCount((uint)contentAlign) != 1)
- {
- return false;
- }
-
- // to calculate:
- // foreach (int val in Enum.GetValues(typeof(ContentAlignment))) { mask |= val; }
- int contentAlignmentMask = 0x777;
- return ((contentAlignmentMask & (int)contentAlign) != 0);
- }
-
///
/// shifts off the number of bits specified by numBitsToShift
/// - makes sure the bits we've shifted off are just zeros
diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LabelTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LabelTests.cs
index c5fddc35e65..8eb159ce779 100644
--- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LabelTests.cs
+++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LabelTests.cs
@@ -2,9 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms.Design;
+using WinForms.Common.Tests;
using Xunit;
using static Interop;
@@ -242,6 +244,48 @@ public void Label_Invokes_SetToolTip_IfExternalToolTipIsSet()
Assert.True(actual);
}
+ [WinFormsTheory]
+ [CommonMemberData(nameof(CommonTestHelper.GetEnumTypeTheoryDataInvalid), typeof(ContentAlignment))]
+ public void Label_ImageAlign_SetInvalidValue_ThrowsInvalidEnumArgumentException(ContentAlignment value)
+ {
+ using var control = new Label();
+ Assert.Throws("value", () => control.ImageAlign = value);
+ }
+
+ public static IEnumerable