From ee4d91eb4ef44c0385c2d63ac40d1a8f49204220 Mon Sep 17 00:00:00 2001 From: gpetrou <4172445+gpetrou@users.noreply.github.com> Date: Thu, 24 Feb 2022 19:40:23 +0200 Subject: [PATCH] Enable nullability in DomainUpDown (#6741) * Enable nullability in DomainUpDown * Rename owner to _owner * Fix warning in DomainItemListAccessibleObject --- .../src/PublicAPI.Shipped.txt | 30 +++++++------- ...inUpDown.DomainItemListAccessibleObject.cs | 2 +- ...DomainUpDown.DomainUpDownItemCollection.cs | 41 +++++++++---------- .../DomainUpDown.DomainUpDownItemCompare.cs | 4 +- .../src/System/Windows/Forms/DomainUpDown.cs | 28 +++++-------- 5 files changed, 47 insertions(+), 58 deletions(-) diff --git a/src/System.Windows.Forms/src/PublicAPI.Shipped.txt b/src/System.Windows.Forms/src/PublicAPI.Shipped.txt index 13ab9a863e..8dcf6b2dd3 100644 --- a/src/System.Windows.Forms/src/PublicAPI.Shipped.txt +++ b/src/System.Windows.Forms/src/PublicAPI.Shipped.txt @@ -399,15 +399,15 @@ override System.Windows.Forms.CursorConverter.GetStandardValuesSupported(System. ~override System.Windows.Forms.Design.ComponentEditorPage.CreateParams.get -> System.Windows.Forms.CreateParams ~override System.Windows.Forms.Design.ToolStripItemDesignerAvailabilityAttribute.Equals(object obj) -> bool ~override System.Windows.Forms.Design.WindowsFormsComponentEditor.EditComponent(System.ComponentModel.ITypeDescriptorContext context, object component) -> bool -~override System.Windows.Forms.DomainUpDown.CreateAccessibilityInstance() -> System.Windows.Forms.AccessibleObject -~override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.Add(object item) -> int -~override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.Insert(int index, object item) -> void -~override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.Remove(object item) -> void -~override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.this[int index].get -> object -~override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.this[int index].set -> void -~override System.Windows.Forms.DomainUpDown.OnChanged(object source, System.EventArgs e) -> void -~override System.Windows.Forms.DomainUpDown.OnTextBoxKeyPress(object source, System.Windows.Forms.KeyPressEventArgs e) -> void -~override System.Windows.Forms.DomainUpDown.ToString() -> string +override System.Windows.Forms.DomainUpDown.CreateAccessibilityInstance() -> System.Windows.Forms.AccessibleObject! +override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.Add(object? item) -> int +override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.Insert(int index, object? item) -> void +override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.Remove(object? item) -> void +override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.this[int index].get -> object? +override System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection.this[int index].set -> void +override System.Windows.Forms.DomainUpDown.OnChanged(object? source, System.EventArgs! e) -> void +override System.Windows.Forms.DomainUpDown.OnTextBoxKeyPress(object? source, System.Windows.Forms.KeyPressEventArgs! e) -> void +override System.Windows.Forms.DomainUpDown.ToString() -> string! ~override System.Windows.Forms.ErrorProvider.Site.set -> void override System.Windows.Forms.FileDialog.ToString() -> string! ~override System.Windows.Forms.FlowLayoutPanel.LayoutEngine.get -> System.Windows.Forms.Layout.LayoutEngine @@ -1723,10 +1723,10 @@ System.Windows.Forms.DataObject.DataObject(string! format, object! data) -> void ~System.Windows.Forms.Design.IUIService.ShowMessage(string message, string caption, System.Windows.Forms.MessageBoxButtons buttons) -> System.Windows.Forms.DialogResult ~System.Windows.Forms.Design.IUIService.Styles.get -> System.Collections.IDictionary ~System.Windows.Forms.Design.WindowsFormsComponentEditor.EditComponent(object component, System.Windows.Forms.IWin32Window owner) -> bool -~System.Windows.Forms.DomainUpDown.Items.get -> System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection -~System.Windows.Forms.DomainUpDown.OnSelectedItemChanged(object source, System.EventArgs e) -> void -~System.Windows.Forms.DomainUpDown.SelectedItem.get -> object -~System.Windows.Forms.DomainUpDown.SelectedItem.set -> void +System.Windows.Forms.DomainUpDown.Items.get -> System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection! +System.Windows.Forms.DomainUpDown.OnSelectedItemChanged(object? source, System.EventArgs! e) -> void +System.Windows.Forms.DomainUpDown.SelectedItem.get -> object? +System.Windows.Forms.DomainUpDown.SelectedItem.set -> void ~System.Windows.Forms.ErrorProvider.BindToDataAndErrors(object newDataSource, string newDataMember) -> void ~System.Windows.Forms.ErrorProvider.CanExtend(object extendee) -> bool ~System.Windows.Forms.ErrorProvider.ContainerControl.get -> System.Windows.Forms.ContainerControl @@ -7766,10 +7766,10 @@ System.Windows.Forms.DomainUpDown.DomainUpDownAccessibleObject.DomainUpDownAcces System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection System.Windows.Forms.DomainUpDown.Padding.get -> System.Windows.Forms.Padding System.Windows.Forms.DomainUpDown.Padding.set -> void -System.Windows.Forms.DomainUpDown.PaddingChanged -> System.EventHandler +System.Windows.Forms.DomainUpDown.PaddingChanged -> System.EventHandler? System.Windows.Forms.DomainUpDown.SelectedIndex.get -> int System.Windows.Forms.DomainUpDown.SelectedIndex.set -> void -System.Windows.Forms.DomainUpDown.SelectedItemChanged -> System.EventHandler +System.Windows.Forms.DomainUpDown.SelectedItemChanged -> System.EventHandler? System.Windows.Forms.DomainUpDown.Sorted.get -> bool System.Windows.Forms.DomainUpDown.Sorted.set -> void System.Windows.Forms.DomainUpDown.Wrap.get -> bool diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainItemListAccessibleObject.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainItemListAccessibleObject.cs index 8e11ddb84f..06e25e4b9b 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainItemListAccessibleObject.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainItemListAccessibleObject.cs @@ -61,7 +61,7 @@ namespace System.Windows.Forms { if (index >= 0 && index < GetChildCount()) { - return new DomainItemAccessibleObject(((DomainUpDown)_parent.Owner).Items[index].ToString(), this); + return new DomainItemAccessibleObject(((DomainUpDown)_parent.Owner).Items[index]!.ToString(), this); } return null; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainUpDownItemCollection.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainUpDownItemCollection.cs index 152328e447..498586075f 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainUpDownItemCollection.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainUpDownItemCollection.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Collections; using System.ComponentModel; @@ -17,17 +15,17 @@ namespace System.Windows.Forms /// public class DomainUpDownItemCollection : ArrayList { - readonly DomainUpDown owner; + private readonly DomainUpDown _owner; internal DomainUpDownItemCollection(DomainUpDown owner) - : base() + : base() { - this.owner = owner; + _owner = owner; } [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] - public override object this[int index] + public override object? this[int index] { get { @@ -38,28 +36,28 @@ namespace System.Windows.Forms { base[index] = value; - if (owner.SelectedIndex == index) + if (_owner.SelectedIndex == index) { - owner.SelectIndex(index); + _owner.SelectIndex(index); } - if (owner.Sorted) + if (_owner.Sorted) { - owner.SortDomainItems(); + _owner.SortDomainItems(); } } } /// /// - public override int Add(object item) + public override int Add(object? item) { // Overridden to perform sorting after adding an item int ret = base.Add(item); - if (owner.Sorted) + if (_owner.Sorted) { - owner.SortDomainItems(); + _owner.SortDomainItems(); } return ret; @@ -67,7 +65,7 @@ namespace System.Windows.Forms /// /// - public override void Remove(object item) + public override void Remove(object? item) { int index = IndexOf(item); @@ -88,27 +86,26 @@ namespace System.Windows.Forms // Overridden to update the domain index if necessary base.RemoveAt(item); - if (item < owner._domainIndex) + if (item < _owner._domainIndex) { // The item removed was before the currently selected item - owner.SelectIndex(owner._domainIndex - 1); + _owner.SelectIndex(_owner._domainIndex - 1); } - else if (item == owner._domainIndex) + else if (item == _owner._domainIndex) { // The currently selected item was removed - // - owner.SelectIndex(-1); + _owner.SelectIndex(-1); } } /// /// - public override void Insert(int index, object item) + public override void Insert(int index, object? item) { base.Insert(index, item); - if (owner.Sorted) + if (_owner.Sorted) { - owner.SortDomainItems(); + _owner.SortDomainItems(); } } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainUpDownItemCompare.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainUpDownItemCompare.cs index 77b0cc8df0..53158fedc7 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainUpDownItemCompare.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.DomainUpDownItemCompare.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Collections; using System.Globalization; @@ -13,7 +11,7 @@ namespace System.Windows.Forms { private sealed class DomainUpDownItemCompare : IComparer { - public int Compare(object p, object q) + public int Compare(object? p, object? q) { if (p == q) { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.cs index 9299f7a7bc..6c61811b74 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Collections; using System.ComponentModel; using System.Diagnostics; @@ -28,13 +26,13 @@ namespace System.Windows.Forms /// /// Allowable strings for the domain updown. /// - private DomainUpDownItemCollection _domainItems; + private DomainUpDownItemCollection? _domainItems; private string _stringValue = s_defaultValue; // Current string value private int _domainIndex = -1; // Index in the domain list private bool _sorted; // Sort the domain values - private EventHandler _onSelectedItemChanged; + private EventHandler? _onSelectedItemChanged; private bool _inSort; @@ -83,7 +81,7 @@ namespace System.Windows.Forms [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] - public new event EventHandler PaddingChanged + public new event EventHandler? PaddingChanged { add => base.PaddingChanged += value; remove => base.PaddingChanged -= value; @@ -132,7 +130,7 @@ namespace System.Windows.Forms [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [SRDescription(nameof(SR.DomainUpDownSelectedItemDescr))] - public object SelectedItem + public object? SelectedItem { get { @@ -142,7 +140,6 @@ namespace System.Windows.Forms set { // Treat null as selecting no item - // if (value is null) { SelectedIndex = -1; @@ -150,7 +147,6 @@ namespace System.Windows.Forms else { // Attempt to find the given item in the list of items - // for (int i = 0; i < Items.Count; i++) { if (value.Equals(Items[i])) @@ -208,7 +204,7 @@ namespace System.Windows.Forms /// [SRCategory(nameof(SR.CatBehavior))] [SRDescription(nameof(SR.DomainUpDownOnSelectedItemChangedDescr))] - public event EventHandler SelectedItemChanged + public event EventHandler? SelectedItemChanged { add => _onSelectedItemChanged += value; remove => _onSelectedItemChanged -= value; @@ -229,7 +225,6 @@ namespace System.Windows.Forms public override void DownButton() { // Make sure domain values exist, and there are >0 items - // if (_domainItems is null) { return; @@ -326,11 +321,11 @@ namespace System.Windows.Forms { if (complete) { - found = Items[index].ToString().Equals(text); + found = Items[index]!.ToString()!.Equals(text); } else { - found = Items[index].ToString().ToUpper(CultureInfo.InvariantCulture).StartsWith(text); + found = Items[index]!.ToString()!.ToUpper(CultureInfo.InvariantCulture).StartsWith(text); } if (found) @@ -355,7 +350,7 @@ namespace System.Windows.Forms /// values is called OnSelectedItemChanged - so just forward it to that /// function. /// - protected override void OnChanged(object source, EventArgs e) + protected override void OnChanged(object? source, EventArgs e) { OnSelectedItemChanged(source, e); } @@ -365,7 +360,7 @@ namespace System.Windows.Forms /// event, using the input character to find the next matching item in our /// item collection. /// - protected override void OnTextBoxKeyPress(object source, KeyPressEventArgs e) + protected override void OnTextBoxKeyPress(object? source, KeyPressEventArgs e) { if (ReadOnly) { @@ -398,7 +393,7 @@ namespace System.Windows.Forms /// /// Raises the event. /// - protected void OnSelectedItemChanged(object source, EventArgs e) + protected void OnSelectedItemChanged(object? source, EventArgs e) { // Call the event handler _onSelectedItemChanged?.Invoke(this, e); @@ -421,11 +416,10 @@ namespace System.Windows.Forms } // If the selected index has changed, update the text - // _domainIndex = index; if (_domainIndex >= 0) { - _stringValue = _domainItems[_domainIndex].ToString(); + _stringValue = _domainItems[_domainIndex]!.ToString()!; UserEdit = false; UpdateEditText(); }