diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.Ime.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.Ime.cs index 0871885a13d..9d2c131d6c7 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.Ime.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.Ime.cs @@ -230,14 +230,14 @@ internal int ImeWmCharsToIgnore // after all messages are sent, corresponding WM_CHAR messages are also sent. (in non-unicode // windows two WM_CHAR messages are sent per char in the IME). We need to keep a counter // not to process each character twice or more. - get => Properties.GetInteger(s_imeWmCharsToIgnoreProperty); + get => Properties.GetValueOrDefault(s_imeWmCharsToIgnoreProperty); set { // WM_CHAR is not send after WM_IME_CHAR when the composition has been closed by either, changing the conversion mode or // dissociating the IME (for instance when loosing focus and conversion is forced to complete). if (ImeWmCharsToIgnore != ImeCharsToIgnoreDisabled) { - Properties.SetInteger(s_imeWmCharsToIgnoreProperty, value); + Properties.AddValue(s_imeWmCharsToIgnoreProperty, value); } } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs index a1f4ed6c17e..0225465f635 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs @@ -758,14 +758,14 @@ public virtual object? DataContext // When DataContext was different than its parent before, but now it is about to become the same, // we're removing it altogether, so it can inherit the value from its parent. - if (Properties.ContainsObject(s_dataContextProperty) && Equals(ParentInternal?.DataContext, value)) + if (Properties.ContainsKey(s_dataContextProperty) && Equals(ParentInternal?.DataContext, value)) { - Properties.RemoveObject(s_dataContextProperty); + Properties.RemoveValue(s_dataContextProperty); OnDataContextChanged(EventArgs.Empty); return; } - Properties.SetObject(s_dataContextProperty, value); + Properties.AddValue(s_dataContextProperty, value); OnDataContextChanged(EventArgs.Empty); } } @@ -774,7 +774,7 @@ private bool ShouldSerializeDataContext() => Properties.ContainsObject(s_dataContextProperty); private void ResetDataContext() - => Properties.RemoveObject(s_dataContextProperty); + => Properties.RemoveValue(s_dataContextProperty); /// /// The background color of this control. This is an ambient property and @@ -834,9 +834,9 @@ public virtual Color BackColor } Color c = BackColor; - if (!value.IsEmpty || Properties.ContainsObject(s_backColorProperty)) + if (!value.IsEmpty || Properties.ContainsKey(s_backColorProperty)) { - Properties.SetColor(s_backColorProperty, value); + Properties.AddValue(s_backColorProperty, value); } if (!c.Equals(BackColor)) @@ -2095,7 +2095,7 @@ public virtual Color ForeColor { get { - Color color = Properties.GetColor(s_foreColorProperty); + Properties.TryGetValue(s_foreColorProperty, out Color color); if (!color.IsEmpty) { return color; @@ -2125,9 +2125,9 @@ public virtual Color ForeColor set { Color color = ForeColor; - if (!value.IsEmpty || Properties.ContainsObject(s_foreColorProperty)) + if (!value.IsEmpty || Properties.ContainsKey(s_foreColorProperty)) { - Properties.SetColor(s_foreColorProperty, value); + Properties.AddValue(s_foreColorProperty, value); } if (!color.Equals(ForeColor)) @@ -2794,7 +2794,7 @@ internal virtual Control? ParentInternal internal PropertyStore Properties { get; } // Returns the value of the backColor field -- no asking the parent with its color is, etc. - internal Color RawBackColor => Properties.GetColor(s_backColorProperty); + internal Color RawBackColor => Properties.GetValueOrDefault(s_backColorProperty); /// /// Indicates whether the control is currently recreating its handle. This @@ -6976,9 +6976,9 @@ protected virtual void OnFontChanged(EventArgs e) Invalidate(); - if (Properties.ContainsInteger(s_fontHeightProperty)) + if (Properties.ContainsKey(s_fontHeightProperty)) { - Properties.SetInteger(s_fontHeightProperty, -1); + Properties.AddValue(s_fontHeightProperty, -1); } // Cleanup any font handle wrapper. @@ -7086,7 +7086,7 @@ protected virtual void OnNotifyMessage(Message m) [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnParentBackColorChanged(EventArgs e) { - Color backColor = Properties.GetColor(s_backColorProperty); + Color backColor = Properties.GetValueOrDefault(s_backColorProperty); if (backColor.IsEmpty) { OnBackColorChanged(e); @@ -7123,10 +7123,10 @@ protected virtual void OnParentDataContextChanged(EventArgs e) if (Properties.ContainsObject(s_dataContextProperty)) { // If this DataContext was the same as the Parent's just became, - if (Equals(Properties.GetObject(s_dataContextProperty), Parent?.DataContext)) + if (Equals(Properties.GetValueOrDefault(s_dataContextProperty), Parent?.DataContext)) { // we need to make it ambient again by removing it. - Properties.RemoveObject(s_dataContextProperty); + Properties.RemoveValue(s_dataContextProperty); // Even though internally we don't store it any longer, and the // value we had stored therefore changed, technically the value @@ -7209,7 +7209,7 @@ internal virtual void OnParentHandleRecreating() // Move this control over to the parking window. // If we left it parented to the parent control, DestroyWindow would force us to destroy our handle as well. - // Temporarilty parenting to the parking window avoids having to recreate our handle from scratch. + // Temporarily parenting to the parking window avoids having to recreate our handle from scratch. if (IsHandleCreated) { @@ -7220,7 +7220,7 @@ internal virtual void OnParentHandleRecreating() [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnParentForeColorChanged(EventArgs e) { - Color foreColor = Properties.GetColor(s_foreColorProperty); + Color foreColor = Properties.GetValueOrDefault(s_foreColorProperty); if (foreColor.IsEmpty) { OnForeColorChanged(e); @@ -10686,7 +10686,7 @@ internal virtual bool ShouldPerformContainerValidation() [EditorBrowsable(EditorBrowsableState.Never)] internal virtual bool ShouldSerializeBackColor() { - Color backColor = Properties.GetColor(s_backColorProperty); + Color backColor = Properties.GetValueOrDefault(s_backColorProperty); return !backColor.IsEmpty; } @@ -10714,7 +10714,7 @@ private bool ShouldSerializeEnabled() [EditorBrowsable(EditorBrowsableState.Never)] internal virtual bool ShouldSerializeForeColor() { - Color foreColor = Properties.GetColor(s_foreColorProperty); + Color foreColor = Properties.GetValueOrDefault(s_foreColorProperty); return !foreColor.IsEmpty; } @@ -10936,9 +10936,9 @@ private protected void SetScaledFont(Font scaledFont, bool raiseOnFontChangedEve // Dispose old FontHandle. DisposeFontHandle(); - if (Properties.ContainsInteger(s_fontHeightProperty)) + if (Properties.ContainsKey(s_fontHeightProperty)) { - Properties.SetInteger(s_fontHeightProperty, scaledFont.Height); + Properties.AddValue(s_fontHeightProperty, scaledFont.Height); } if (!raiseOnFontChangedEvent) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.cs index c9baff3b0bf..4bfddc81cf2 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.cs @@ -447,18 +447,14 @@ public DrawMode DrawMode [SRDescription(nameof(SR.ComboBoxDropDownWidthDescr))] public int DropDownWidth { - get - { - int dropDownWidth = Properties.GetInteger(s_propDropDownWidth, out bool found); - return found ? dropDownWidth : Width; - } + get => Properties.TryGetValue(s_propDropDownWidth, out int dropDownWidth) ? dropDownWidth : Width; set { ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value); - if (Properties.GetInteger(s_propDropDownWidth) != value) + if (Properties.GetValueOrDefault(s_propDropDownWidth) != value) { - Properties.SetInteger(s_propDropDownWidth, value); + Properties.AddValue(s_propDropDownWidth, value); if (IsHandleCreated) { PInvoke.SendMessage(this, PInvoke.CB_SETDROPPEDWIDTH, (WPARAM)value); @@ -477,35 +473,24 @@ public int DropDownWidth [DefaultValue(106)] public int DropDownHeight { - get - { - int dropDownHeight = Properties.GetInteger(s_propDropDownHeight, out bool found); - if (found) - { - return dropDownHeight; - } - else - { - return DefaultDropDownHeight; - } - } + get => Properties.TryGetValue(s_propDropDownHeight, out int dropDownHeight) ? dropDownHeight : DefaultDropDownHeight; set { ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value); - if (Properties.GetInteger(s_propDropDownHeight) != value) + if (Properties.GetValueOrDefault(s_propDropDownHeight) != value) { - Properties.SetInteger(s_propDropDownHeight, value); + Properties.AddValue(s_propDropDownHeight, value); // The dropDownHeight is not reflected unless the - // combobox integralHeight == false.. + // ComboBox integralHeight == false.. IntegralHeight = false; } } } /// - /// Indicates whether the DropDown of the combo is currently dropped down. + /// Indicates whether the DropDown of the combo is currently dropped down. /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] @@ -636,27 +621,19 @@ public int ItemHeight drawMode == DrawMode.OwnerDrawVariable || !IsHandleCreated) { - int itemHeight = Properties.GetInteger(s_propItemHeight, out bool found); - if (found) - { - return itemHeight; - } - else - { - return FontHeight + 2; - } + return Properties.TryGetValue(s_propItemHeight, out int itemHeight) ? itemHeight : FontHeight + 2; } // Note that the above if clause deals with the case when the handle has not yet been created Debug.Assert(IsHandleCreated, "Handle should be created at this point"); - int h = (int)PInvoke.SendMessage(this, PInvoke.CB_GETITEMHEIGHT); - if (h == -1) + int height = (int)PInvoke.SendMessage(this, PInvoke.CB_GETITEMHEIGHT); + if (height == -1) { throw new Win32Exception(); } - return h; + return height; } set { @@ -664,9 +641,9 @@ public int ItemHeight ResetHeightCache(); - if (Properties.GetInteger(s_propItemHeight) != value) + if (Properties.GetValueOrDefault(s_propItemHeight) != value) { - Properties.SetInteger(s_propItemHeight, value); + Properties.AddValue(s_propItemHeight, value); if (DrawMode != DrawMode.Normal) { UpdateItemHeight(); @@ -759,7 +736,7 @@ public int MaxLength { get { - return Properties.GetInteger(s_propMaxLength); + return Properties.GetValueOrDefault(s_propMaxLength); } set { @@ -770,7 +747,7 @@ public int MaxLength if (MaxLength != value) { - Properties.SetInteger(s_propMaxLength, value); + Properties.AddValue(s_propMaxLength, value); if (IsHandleCreated) { PInvoke.SendMessage(this, PInvoke.CB_LIMITTEXT, (WPARAM)value); @@ -3201,12 +3178,12 @@ private void ResetAutoCompleteCustomSource() private void ResetDropDownWidth() { - Properties.RemoveInteger(s_propDropDownWidth); + Properties.RemoveValue(s_propDropDownWidth); } private void ResetItemHeight() { - Properties.RemoveInteger(s_propItemHeight); + Properties.RemoveValue(s_propItemHeight); } public override void ResetText() @@ -3433,7 +3410,7 @@ private bool ShouldSerializeAutoCompleteCustomSource() internal bool ShouldSerializeDropDownWidth() { - return (Properties.ContainsInteger(s_propDropDownWidth)); + return (Properties.ContainsKey(s_propDropDownWidth)); } /// @@ -3441,7 +3418,7 @@ internal bool ShouldSerializeDropDownWidth() /// internal bool ShouldSerializeItemHeight() { - return (Properties.ContainsInteger(s_propItemHeight)); + return (Properties.ContainsKey(s_propItemHeight)); } /// diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewCellStyle.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewCellStyle.cs index ae52815972f..60aa141cb2a 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewCellStyle.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewCellStyle.cs @@ -115,13 +115,13 @@ internal DataGridViewContentAlignment AlignmentInternal [SRCategory(nameof(SR.CatAppearance))] public Color BackColor { - get => Properties.GetColor(s_propBackColor); + get => Properties.GetValueOrDefault(s_propBackColor); set { Color c = BackColor; - if (!value.IsEmpty || Properties.ContainsObject(s_propBackColor)) + if (!value.IsEmpty || Properties.ContainsKey(s_propBackColor)) { - Properties.SetColor(s_propBackColor, value); + Properties.AddValue(s_propBackColor, value); } if (!c.Equals(BackColor)) @@ -150,13 +150,13 @@ public object? DataSourceNullValue } if (value == DBNull.Value && - Properties.ContainsObject(s_propDataSourceNullValue)) + Properties.ContainsKey(s_propDataSourceNullValue)) { - Properties.RemoveObject(s_propDataSourceNullValue); + Properties.RemoveValue(s_propDataSourceNullValue); } else { - Properties.SetObject(s_propDataSourceNullValue, value); + Properties.AddValue(s_propDataSourceNullValue, value); } Debug.Assert((oldDataSourceNullValue is null && DataSourceNullValue is not null) || @@ -318,13 +318,13 @@ public object? NullValue return; } - if (value is string stringValue && stringValue.Length == 0 && Properties.ContainsObject(s_propNullValue)) + if (value is string stringValue && stringValue.Length == 0 && Properties.ContainsKey(s_propNullValue)) { - Properties.RemoveObject(s_propNullValue); + Properties.RemoveValue(s_propNullValue); } else { - Properties.SetObject(s_propNullValue, value); + Properties.AddValue(s_propNullValue, value); } Debug.Assert((oldNullValue is null && NullValue is not null) || @@ -384,13 +384,13 @@ internal Padding PaddingInternal [SRCategory(nameof(SR.CatAppearance))] public Color SelectionBackColor { - get => Properties.GetColor(s_propSelectionBackColor); + get => Properties.GetValueOrDefault(s_propSelectionBackColor); set { Color c = SelectionBackColor; - if (!value.IsEmpty || Properties.ContainsObject(s_propSelectionBackColor)) + if (!value.IsEmpty || Properties.ContainsKey(s_propSelectionBackColor)) { - Properties.SetColor(s_propSelectionBackColor, value); + Properties.AddValue(s_propSelectionBackColor, value); } if (!c.Equals(SelectionBackColor)) @@ -403,13 +403,13 @@ public Color SelectionBackColor [SRCategory(nameof(SR.CatAppearance))] public Color SelectionForeColor { - get => Properties.GetColor(s_propSelectionForeColor); + get => Properties.GetValueOrDefault(s_propSelectionForeColor); set { Color c = SelectionForeColor; - if (!value.IsEmpty || Properties.ContainsObject(s_propSelectionForeColor)) + if (!value.IsEmpty || Properties.ContainsKey(s_propSelectionForeColor)) { - Properties.SetColor(s_propSelectionForeColor, value); + Properties.AddValue(s_propSelectionForeColor, value); } if (!c.Equals(SelectionForeColor)) @@ -636,11 +636,8 @@ private bool ShouldSerializeSelectionBackColor() return found; } - private bool ShouldSerializeSelectionForeColor() - { - Properties.GetColor(s_propSelectionForeColor, out bool found); - return found; - } + private bool ShouldSerializeSelectionForeColor() => + Properties.ContainsKey(s_propSelectionForeColor); public override string ToString() { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewLinkCell.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewLinkCell.cs index 89befeeb995..ff95c93ba60 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewLinkCell.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewLinkCell.cs @@ -442,32 +442,32 @@ public override object Clone() CloneInternal(dataGridViewCell); - if (Properties.ContainsObject(s_propLinkCellActiveLinkColor)) + if (Properties.ContainsKey(s_propLinkCellActiveLinkColor)) { dataGridViewCell.ActiveLinkColorInternal = ActiveLinkColor; } - if (Properties.ContainsInteger(s_propLinkCellUseColumnTextForLinkValue)) + if (Properties.ContainsKey(s_propLinkCellUseColumnTextForLinkValue)) { dataGridViewCell.UseColumnTextForLinkValueInternal = UseColumnTextForLinkValue; } - if (Properties.ContainsInteger(s_propLinkCellLinkBehavior)) + if (Properties.ContainsKey(s_propLinkCellLinkBehavior)) { dataGridViewCell.LinkBehaviorInternal = LinkBehavior; } - if (Properties.ContainsObject(s_propLinkCellLinkColor)) + if (Properties.ContainsKey(s_propLinkCellLinkColor)) { dataGridViewCell.LinkColorInternal = LinkColor; } - if (Properties.ContainsInteger(s_propLinkCellTrackVisitedState)) + if (Properties.ContainsKey(s_propLinkCellTrackVisitedState)) { dataGridViewCell.TrackVisitedStateInternal = TrackVisitedState; } - if (Properties.ContainsObject(s_propLinkCellVisitedLinkColor)) + if (Properties.ContainsKey(s_propLinkCellVisitedLinkColor)) { dataGridViewCell.VisitedLinkColorInternal = VisitedLinkColor; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripItem.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripItem.cs index a7b91ba59a1..83287533fa9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripItem.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripItem.cs @@ -539,9 +539,9 @@ public virtual Color BackColor set { Color c = BackColor; - if (!value.IsEmpty || Properties.ContainsObject(s_backColorProperty)) + if (!value.IsEmpty || Properties.ContainsKey(s_backColorProperty)) { - Properties.SetColor(s_backColorProperty, value); + Properties.AddValue(s_backColorProperty, value); } if (!c.Equals(BackColor)) @@ -880,7 +880,7 @@ public virtual Color ForeColor { get { - Color foreColor = Properties.GetColor(s_foreColorProperty); + Color foreColor = Properties.GetValueOrDefault(s_foreColorProperty); if (!foreColor.IsEmpty) { return foreColor; @@ -897,9 +897,9 @@ public virtual Color ForeColor set { Color c = ForeColor; - if (!value.IsEmpty || Properties.ContainsObject(s_foreColorProperty)) + if (!value.IsEmpty || Properties.ContainsKey(s_foreColorProperty)) { - Properties.SetColor(s_foreColorProperty, value); + Properties.AddValue(s_foreColorProperty, value); } if (!c.Equals(ForeColor)) @@ -1218,7 +1218,7 @@ internal bool IsForeColorSet { get { - Color color = Properties.GetColor(s_foreColorProperty); + Color color = Properties.GetValueOrDefault(s_foreColorProperty); if (!color.IsEmpty) { return true; @@ -1658,7 +1658,7 @@ public event QueryAccessibilityHelpEventHandler? QueryAccessibilityHelp /// /// Returns the value of the backColor field -- no asking the parent with its color is, etc. /// - internal Color RawBackColor => Properties.GetColor(s_backColorProperty); + internal Color RawBackColor => Properties.GetValueOrDefault(s_backColorProperty); /// /// Returns the parent 's renderer @@ -1688,30 +1688,29 @@ public virtual RightToLeft RightToLeft { get { - int rightToLeft = Properties.GetInteger(s_rightToLeftProperty, out bool found); - if (!found) + if (!Properties.TryGetValue(s_rightToLeftProperty, out RightToLeft rightToLeft)) { - rightToLeft = (int)RightToLeft.Inherit; + rightToLeft = RightToLeft.Inherit; } - if (((RightToLeft)rightToLeft) == RightToLeft.Inherit) + if (rightToLeft == RightToLeft.Inherit) { if (Owner is not null) { - rightToLeft = (int)Owner.RightToLeft; + rightToLeft = Owner.RightToLeft; } else if (ParentInternal is not null) { // case for Overflow & Grip - rightToLeft = (int)ParentInternal.RightToLeft; + rightToLeft = ParentInternal.RightToLeft; } else { - rightToLeft = (int)DefaultRightToLeft; + rightToLeft = DefaultRightToLeft; } } - return (RightToLeft)rightToLeft; + return rightToLeft; } set @@ -1720,9 +1719,9 @@ public virtual RightToLeft RightToLeft RightToLeft oldValue = RightToLeft; - if (Properties.ContainsInteger(s_rightToLeftProperty) || value != RightToLeft.Inherit) + if (Properties.ContainsKey(s_rightToLeftProperty) || value != RightToLeft.Inherit) { - Properties.SetInteger(s_rightToLeftProperty, (int)value); + Properties.AddValue(s_rightToLeftProperty, value); } if (oldValue != RightToLeft) @@ -2932,7 +2931,7 @@ protected virtual void OnPaint(PaintEventArgs e) [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnParentBackColorChanged(EventArgs e) { - Color backColor = Properties.GetColor(s_backColorProperty); + Color backColor = Properties.GetValueOrDefault(s_backColorProperty); if (backColor.IsEmpty) { OnBackColorChanged(e); @@ -2980,7 +2979,7 @@ protected internal virtual void OnOwnerFontChanged(EventArgs e) [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnParentForeColorChanged(EventArgs e) { - Color foreColor = Properties.GetColor(s_foreColorProperty); + Color foreColor = Properties.GetValueOrDefault(s_foreColorProperty); if (foreColor.IsEmpty) { OnForeColorChanged(e); @@ -2990,7 +2989,7 @@ protected virtual void OnParentForeColorChanged(EventArgs e) [EditorBrowsable(EditorBrowsableState.Advanced)] protected internal virtual void OnParentRightToLeftChanged(EventArgs e) { - if (!Properties.ContainsInteger(s_rightToLeftProperty) || ((RightToLeft)Properties.GetInteger(s_rightToLeftProperty)) == RightToLeft.Inherit) + if (!Properties.TryGetValue(s_rightToLeftProperty, out RightToLeft rightToLeft) || rightToLeft == RightToLeft.Inherit) { OnRightToLeftChanged(e); } @@ -3006,13 +3005,12 @@ protected virtual void OnOwnerChanged(EventArgs e) if (Owner is not null) { // check if we need to fire OnRightToLeftChanged - int rightToLeft = Properties.GetInteger(s_rightToLeftProperty, out bool found); - if (!found) + if (!Properties.TryGetValue(s_rightToLeftProperty, out RightToLeft rightToLeft)) { - rightToLeft = (int)RightToLeft.Inherit; + rightToLeft = RightToLeft.Inherit; } - if ((rightToLeft == (int)RightToLeft.Inherit) && RightToLeft != DefaultRightToLeft) + if (rightToLeft == RightToLeft.Inherit && RightToLeft != DefaultRightToLeft) { OnRightToLeftChanged(EventArgs.Empty); } @@ -3354,7 +3352,7 @@ internal void SetAmbientMargin() [EditorBrowsable(EditorBrowsableState.Never)] internal virtual bool ShouldSerializeBackColor() { - Color backColor = Properties.GetColor(s_backColorProperty); + Color backColor = Properties.GetValueOrDefault(s_backColorProperty); return !backColor.IsEmpty; } @@ -3368,7 +3366,7 @@ internal virtual bool ShouldSerializeBackColor() [EditorBrowsable(EditorBrowsableState.Never)] internal virtual bool ShouldSerializeForeColor() { - Color foreColor = Properties.GetColor(s_foreColorProperty); + Color foreColor = Properties.GetValueOrDefault(s_foreColorProperty); return !foreColor.IsEmpty; } @@ -3422,13 +3420,12 @@ private bool ShouldSerializeImageIndex() [EditorBrowsable(EditorBrowsableState.Never)] internal virtual bool ShouldSerializeRightToLeft() { - int rightToLeft = Properties.GetInteger(s_rightToLeftProperty, out bool found); - if (!found) + if (!Properties.TryGetValue(s_rightToLeftProperty, out RightToLeft rightToLeft)) { return false; } - return rightToLeft != (int)DefaultRightToLeft; + return rightToLeft != DefaultRightToLeft; } private bool ShouldSerializeTextDirection() diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs index 1ba6b394e56..83743a940e5 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs @@ -113,10 +113,8 @@ public partial class Form : ContainerControl private static readonly int s_propMaximizedBounds = PropertyStore.CreateKey(); private static readonly int s_propOwnedFormsCount = PropertyStore.CreateKey(); - private static readonly int s_propMinTrackSizeWidth = PropertyStore.CreateKey(); - private static readonly int s_propMinTrackSizeHeight = PropertyStore.CreateKey(); - private static readonly int s_propMaxTrackSizeWidth = PropertyStore.CreateKey(); - private static readonly int s_propMaxTrackSizeHeight = PropertyStore.CreateKey(); + private static readonly int s_propMinTrackSize = PropertyStore.CreateKey(); + private static readonly int s_propMaxTrackSize = PropertyStore.CreateKey(); private static readonly int s_propFormMdiParent = PropertyStore.CreateKey(); private static readonly int s_propActiveMdiChild = PropertyStore.CreateKey(); @@ -1239,15 +1237,7 @@ public event EventHandler? MaximizedBoundsChanged [DefaultValue(typeof(Size), "0, 0")] public override Size MaximumSize { - get - { - if (Properties.ContainsInteger(s_propMaxTrackSizeWidth)) - { - return new Size(Properties.GetInteger(s_propMaxTrackSizeWidth), Properties.GetInteger(s_propMaxTrackSizeHeight)); - } - - return Size.Empty; - } + get => Properties.TryGetValue(s_propMaxTrackSize, out Size maximumSize) ? maximumSize : Size.Empty; set { if (!value.Equals(MaximumSize)) @@ -1264,20 +1254,28 @@ public override Size MaximumSize private void UpdateMaximumSize(Size value, bool updateFormSize = true) { - Properties.SetInteger(s_propMaxTrackSizeWidth, value.Width); - Properties.SetInteger(s_propMaxTrackSizeHeight, value.Height); + Properties.AddValue(s_propMaxTrackSize, value); // Bump minimum size if necessary if (!MinimumSize.IsEmpty && !value.IsEmpty) { - if (Properties.GetInteger(s_propMinTrackSizeWidth) > value.Width) + Size minimumSize = MinimumSize; + bool sizeUpdated = false; + if (minimumSize.Width > value.Width) + { + minimumSize.Width = value.Width; + sizeUpdated = true; + } + + if (minimumSize.Height > value.Height) { - Properties.SetInteger(s_propMinTrackSizeWidth, value.Width); + minimumSize.Height = value.Height; + sizeUpdated = true; } - if (Properties.GetInteger(s_propMinTrackSizeHeight) > value.Height) + if (sizeUpdated) { - Properties.SetInteger(s_propMinTrackSizeHeight, value.Height); + Properties.AddValue(s_propMinTrackSize, minimumSize); } } @@ -1356,15 +1354,7 @@ public MenuStrip? MainMenuStrip [RefreshProperties(RefreshProperties.Repaint)] public override Size MinimumSize { - get - { - if (Properties.ContainsInteger(s_propMinTrackSizeWidth)) - { - return new Size(Properties.GetInteger(s_propMinTrackSizeWidth), Properties.GetInteger(s_propMinTrackSizeHeight)); - } - - return DefaultMinimumSize; - } + get => Properties.TryGetValue(s_propMinTrackSize, out Size minimumSize) ? minimumSize : DefaultMinimumSize; set { if (!value.Equals(MinimumSize)) @@ -1385,20 +1375,28 @@ public override Size MinimumSize private void UpdateMinimumSize(Size value, bool updateFormSize = true) { - Properties.SetInteger(s_propMinTrackSizeWidth, value.Width); - Properties.SetInteger(s_propMinTrackSizeHeight, value.Height); + Properties.AddValue(s_propMinTrackSize, value); // Bump maximum size if necessary if (!MaximumSize.IsEmpty && !value.IsEmpty) { - if (Properties.GetInteger(s_propMaxTrackSizeWidth) < value.Width) + Size maximumSize = MaximumSize; + bool sizeUpdated = false; + if (maximumSize.Width < value.Width) + { + maximumSize.Width = value.Width; + sizeUpdated = true; + } + + if (maximumSize.Height < value.Height) { - Properties.SetInteger(s_propMaxTrackSizeWidth, value.Width); + maximumSize.Height = value.Height; + sizeUpdated = true; } - if (Properties.GetInteger(s_propMaxTrackSizeHeight) < value.Height) + if (sizeUpdated) { - Properties.SetInteger(s_propMaxTrackSizeHeight, value.Height); + Properties.AddValue(s_propMaxTrackSize, maximumSize); } } @@ -1750,7 +1748,7 @@ public Form[] OwnedForms get { Form?[]? ownedForms = (Form?[]?)Properties.GetObject(s_propOwnedForms); - int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount); + int ownedFormsCount = Properties.GetValueOrDefault(s_propOwnedFormsCount); Form[] result = new Form[ownedFormsCount]; if (ownedFormsCount > 0) @@ -2637,7 +2635,7 @@ public void AddOwnedForm(Form? ownedForm) } Form?[]? ownedForms = (Form?[]?)Properties.GetObject(s_propOwnedForms); - int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount); + int ownedFormsCount = Properties.GetValueOrDefault(s_propOwnedFormsCount); // Make sure this isn't already in the list: for (int i = 0; i < ownedFormsCount; i++) @@ -3326,7 +3324,7 @@ protected override void Dispose(bool disposing) Properties.SetObject(s_propDialogOwner, null); Form?[]? ownedForms = (Form?[]?)Properties.GetObject(s_propOwnedForms); - int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount); + int ownedFormsCount = Properties.GetValueOrDefault(s_propOwnedFormsCount); for (int i = ownedFormsCount - 1; i >= 0; i--) { @@ -3345,7 +3343,7 @@ protected override void Dispose(bool disposing) if (Properties.TryGetObject(s_propDummyMdiMenu, out HMENU dummyMenu) && !dummyMenu.IsNull) { - Properties.RemoveObject(s_propDummyMdiMenu); + Properties.RemoveValue(s_propDummyMdiMenu); PInvoke.DestroyMenu(dummyMenu); } } @@ -4582,7 +4580,7 @@ internal void RaiseFormClosedOnAppExit() { // Fire FormClosed event on all the forms that this form owns and are not in the Application.OpenForms collection // This is to be consistent with what WmClose does. - int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount); + int ownedFormsCount = Properties.GetValueOrDefault(s_propOwnedFormsCount); if (ownedFormsCount > 0) { Form[] ownedForms = OwnedForms; @@ -4612,7 +4610,7 @@ internal bool RaiseFormClosingOnAppExit() { // Fire FormClosing event on all the forms that this form owns and are not in the Application.OpenForms collection // This is to be consistent with what WmClose does. - int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount); + int ownedFormsCount = Properties.GetValueOrDefault(s_propOwnedFormsCount); if (ownedFormsCount > 0) { Form[] ownedForms = OwnedForms; @@ -4714,7 +4712,7 @@ public void RemoveOwnedForm(Form? ownedForm) } Form?[]? ownedForms = (Form?[]?)Properties.GetObject(s_propOwnedForms); - int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount); + int ownedFormsCount = Properties.GetValueOrDefault(s_propOwnedFormsCount); if (ownedForms is not null) { @@ -4736,7 +4734,7 @@ public void RemoveOwnedForm(Form? ownedForm) } } - Properties.SetInteger(s_propOwnedFormsCount, ownedFormsCount); + Properties.AddValue(s_propOwnedFormsCount, ownedFormsCount); } } @@ -6152,7 +6150,7 @@ private void WmClose(ref Message m) // Call OnClosing/OnFormClosing on all the forms that current form owns. Form[] ownedForms = OwnedForms; - int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount); + int ownedFormsCount = Properties.GetValueOrDefault(s_propOwnedFormsCount); for (int i = ownedFormsCount - 1; i >= 0; i--) { FormClosingEventArgs cfe = new(CloseReason.FormOwnerClosing, e.Cancel); @@ -6218,7 +6216,7 @@ private void WmClose(ref Message m) // Call OnClosed/OnFormClosed on all the forms that current form owns. Form[] ownedForms = OwnedForms; - int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount); + int ownedFormsCount = Properties.GetValueOrDefault(s_propOwnedFormsCount); for (int i = ownedFormsCount - 1; i >= 0; i--) { fc = new FormClosedEventArgs(CloseReason.FormOwnerClosing); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/CommonProperties.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/CommonProperties.cs index f1032f4a15a..8a4decc5e7e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/CommonProperties.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/CommonProperties.cs @@ -51,13 +51,7 @@ internal partial class CommonProperties /// /// Removes the maximum size from the property store, making it "unset". /// - internal static void ClearMaximumSize(IArrangedElement element) - { - if (element.Properties.ContainsObject(s_maximumSizeProperty)) - { - element.Properties.RemoveObject(s_maximumSizeProperty); - } - } + internal static void ClearMaximumSize(IArrangedElement element) => element.Properties.RemoveValue(s_maximumSizeProperty); /// /// Determines whether or not the s @@ -102,8 +96,7 @@ internal static Padding GetMargin(IArrangedElement element) /// internal static Size GetMaximumSize(IArrangedElement element, Size defaultMaximumSize) { - Size size = element.Properties.GetSize(s_maximumSizeProperty, out bool found); - if (found) + if (element.Properties.TryGetValue(s_maximumSizeProperty, out Size size)) { return size; } @@ -116,8 +109,7 @@ internal static Size GetMaximumSize(IArrangedElement element, Size defaultMaximu /// internal static Size GetMinimumSize(IArrangedElement element, Size defaultMinimumSize) { - Size size = element.Properties.GetSize(s_minimumSizeProperty, out bool found); - if (found) + if (element.Properties.TryGetValue(s_minimumSizeProperty, out Size size)) { return size; } @@ -213,7 +205,7 @@ internal static void SetMaximumSize(IArrangedElement element, Size value) Debug.Assert(value != GetMaximumSize(element, new Size(-7109, -7107)), "PERF: Caller should guard against setting MaximumSize to original value."); - element.Properties.SetSize(s_maximumSizeProperty, value); + element.Properties.AddValue(s_maximumSizeProperty, value); // Element bounds may need to truncated to new maximum Rectangle bounds = element.Bounds; @@ -235,7 +227,7 @@ internal static void SetMinimumSize(IArrangedElement element, Size value) Debug.Assert(value != GetMinimumSize(element, new Size(-7109, -7107)), "PERF: Caller should guard against setting MinimumSize to original value."); - element.Properties.SetSize(s_minimumSizeProperty, value); + element.Properties.AddValue(s_minimumSizeProperty, value); using (new LayoutTransaction(element.Container as Control, element, PropertyNames.MinimumSize)) { @@ -357,7 +349,7 @@ internal static void UpdateSpecifiedBounds(IArrangedElement element, int x, int /// internal static void xClearPreferredSizeCache(IArrangedElement element) { - element.Properties.SetSize(s_preferredSizeCacheProperty, LayoutUtils.s_invalidSize); + element.Properties.AddValue(s_preferredSizeCacheProperty, LayoutUtils.s_invalidSize); Debug.Assert(xGetPreferredSizeCache(element) == Size.Empty, "Error detected in xClearPreferredSizeCache."); } @@ -387,8 +379,7 @@ internal static void xClearAllPreferredSizeCaches(IArrangedElement start) /// internal static Size xGetPreferredSizeCache(IArrangedElement element) { - Size size = element.Properties.GetSize(s_preferredSizeCacheProperty, out bool found); - if (found && (size != LayoutUtils.s_invalidSize)) + if (element.Properties.TryGetValue(s_preferredSizeCacheProperty, out Size size) && (size != LayoutUtils.s_invalidSize)) { return size; } @@ -404,7 +395,7 @@ internal static void xSetPreferredSizeCache(IArrangedElement element, Size value Debug.Assert( value == Size.Empty || value != xGetPreferredSizeCache(element), "PERF: Caller should guard against setting PreferredSizeCache to original value."); - element.Properties.SetSize(s_preferredSizeCacheProperty, value); + element.Properties.AddValue(s_preferredSizeCacheProperty, value); Debug.Assert(xGetPreferredSizeCache(element) == value, "Error detected in xGetPreferredSizeCache."); } @@ -687,8 +678,7 @@ internal static void SetFlowBreak(IArrangedElement element, bool value) /// internal static Size GetLayoutBounds(IArrangedElement element) { - Size size = element.Properties.GetSize(s_layoutBoundsProperty, out bool found); - if (found) + if (element.Properties.TryGetValue(s_layoutBoundsProperty, out Size size)) { return size; } @@ -711,7 +701,7 @@ internal static Size GetLayoutBounds(IArrangedElement element) /// internal static void SetLayoutBounds(IArrangedElement element, Size value) { - element.Properties.SetSize(s_layoutBoundsProperty, value); + element.Properties.AddValue(s_layoutBoundsProperty, value); } /// @@ -719,8 +709,7 @@ internal static void SetLayoutBounds(IArrangedElement element, Size value) /// internal static bool HasLayoutBounds(IArrangedElement element) { - element.Properties.GetSize(s_layoutBoundsProperty, out bool found); - return found; + return element.Properties.ContainsKey(s_layoutBoundsProperty); } #endregion @@ -736,9 +725,9 @@ internal static bool HasLayoutBounds(IArrangedElement element) /// /// internal static BitVector32 GetLayoutState(IArrangedElement element) => - new BitVector32(element.Properties.GetInteger(s_layoutStateProperty)); + element.Properties.GetValueOrDefault(s_layoutStateProperty); internal static void SetLayoutState(IArrangedElement element, BitVector32 state) => - element.Properties.SetInteger(s_layoutStateProperty, state.Data); + element.Properties.AddValue(s_layoutStateProperty, state); #endregion } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/FlowLayout.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/FlowLayout.cs index d58ab134f18..c0058623770 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/FlowLayout.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/FlowLayout.cs @@ -279,23 +279,23 @@ private static Size TryCalculatePreferredSizeRow( } public static bool GetWrapContents(IArrangedElement container) => - container.Properties.GetInteger(s_wrapContentsProperty) == 0; + !container.Properties.TryGetValue(s_wrapContentsProperty, out bool wrap) || wrap; public static void SetWrapContents(IArrangedElement container, bool value) { - container.Properties.SetInteger(s_wrapContentsProperty, value ? 0 : 1); + container.Properties.AddValue(s_wrapContentsProperty, value); LayoutTransaction.DoLayout(container, container, PropertyNames.WrapContents); Debug.Assert(GetWrapContents(container) == value, "GetWrapContents should return the same value as we set"); } public static FlowDirection GetFlowDirection(IArrangedElement container) => - (FlowDirection)container.Properties.GetInteger(s_flowDirectionProperty); + container.Properties.GetValueOrDefault(s_flowDirectionProperty); public static void SetFlowDirection(IArrangedElement container, FlowDirection value) { SourceGenerated.EnumValidator.Validate(value); - container.Properties.SetInteger(s_flowDirectionProperty, (int)value); + container.Properties.AddValue(s_flowDirectionProperty, value); LayoutTransaction.DoLayout(container, container, PropertyNames.FlowDirection); Debug.Assert(GetFlowDirection(container) == value, "GetFlowDirection should return the same value as we set"); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyStore.cs b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyStore.cs index d9848f5dab1..af8122cda06 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyStore.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyStore.cs @@ -20,9 +20,6 @@ internal class PropertyStore /// public bool ContainsKey(int key) => _values.ContainsKey(key); - // REMOVE - public bool ContainsInteger(int key) => _values.ContainsKey(key); - // REMOVE public bool ContainsObject(int key) => _values.ContainsKey(key); @@ -31,49 +28,6 @@ internal class PropertyStore /// public static int CreateKey() => s_currentKey++; - // REMOVE - public Color GetColor(int key) => GetColor(key, out _); - - // REMOVE - /// - /// A wrapper around GetObject designed to reduce the boxing hit - /// - public Color GetColor(int key, out bool found) - { - if (_values.TryGetValue(key, out Value value)) - { - found = true; - return value.GetValue(); - } - - found = false; - return Color.Empty; - } - - // REMOVE - /// - /// A wrapper around GetObject designed to reduce the boxing hit. - /// - public Size GetSize(int key, out bool found) - { - if (_values.TryGetValue(key, out Value value)) - { - found = true; - return value.GetValue(); - } - - found = false; - return Size.Empty; - } - - // REMOVE - /// - /// Retrieves an integer value from our property list. - /// This will set value to zero and return false if the - /// list does not contain the given key. - /// - public int GetInteger(int key) => GetInteger(key, out _); - // REMOVE /// /// Retrieves an integer value from our property list. @@ -140,29 +94,11 @@ public bool ContainsObjectThatIsNotNull(int key) return found ? value.GetValue() : null; } - // REMOVE - /// - /// Removes the given key from the array - /// - public void RemoveInteger(int key) => _values.Remove(key); - - // REMOVE - /// - /// Removes the given key from the array - /// - public void RemoveObject(int key) => _values.Remove(key); - /// /// Removes the given key from the store. /// public void RemoveValue(int key) => _values.Remove(key); - // REMOVE - public void SetColor(int key, Color value) => _values[key] = value; - - // REMOVE - public void SetSize(int key, Size value) => _values[key] = value; - // REMOVE /// /// Stores the given value in the key. diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PropertyStoreTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PropertyStoreTests.cs index 41d6af46c3f..be520b0db1e 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PropertyStoreTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PropertyStoreTests.cs @@ -77,7 +77,7 @@ public void PropertyStore_TryGetValue_NotExists(int key, object? value) { PropertyStore store = new(); store.SetObject(key, null); - store.RemoveObject(key); + store.RemoveValue(key); Assert.False(store.ContainsObject(key), "PropertyStore contains key."); Assert.False(store.TryGetObject(key, out object? outValue), "PropertyStore contains key."); Assert.NotEqual(value, outValue);