From 1030aacf935f880bd67f385e365207cfb8caac38 Mon Sep 17 00:00:00 2001 From: gpetrou <4172445+gpetrou@users.noreply.github.com> Date: Thu, 17 Nov 2022 19:44:06 +0200 Subject: [PATCH] Enable nullability in some ToolStripItem members (#8174) --- .../src/PublicAPI.Shipped.txt | 78 +++++++++---------- .../Windows/Forms/ToolStripDropDownItem.cs | 2 +- ...StripItem.ToolStripItemAccessibleObject.cs | 2 +- .../src/System/Windows/Forms/ToolStripItem.cs | 67 ++++++++-------- .../System/Windows/Forms/ToolStripMenuItem.cs | 2 +- .../Windows/Forms/ToolStripSeparator.cs | 3 +- .../Windows/Forms/ToolStripSystemRenderer.cs | 2 +- .../src/System/Windows/Forms/WinFormsUtils.cs | 3 +- 8 files changed, 79 insertions(+), 80 deletions(-) diff --git a/src/System.Windows.Forms/src/PublicAPI.Shipped.txt b/src/System.Windows.Forms/src/PublicAPI.Shipped.txt index 1f19814b56e..eab308590aa 100644 --- a/src/System.Windows.Forms/src/PublicAPI.Shipped.txt +++ b/src/System.Windows.Forms/src/PublicAPI.Shipped.txt @@ -534,7 +534,7 @@ override System.Windows.Forms.ToolStripSeparator.Image.get -> System.Drawing.Ima override System.Windows.Forms.ToolStripSeparator.Image.set -> void override System.Windows.Forms.ToolStripSeparator.OnFontChanged(System.EventArgs! e) -> void override System.Windows.Forms.ToolStripSeparator.OnPaint(System.Windows.Forms.PaintEventArgs! e) -> void -override System.Windows.Forms.ToolStripSeparator.Text.get -> string! +override System.Windows.Forms.ToolStripSeparator.Text.get -> string? override System.Windows.Forms.ToolStripSeparator.Text.set -> void override System.Windows.Forms.ToolStripSplitButton.CreateAccessibilityInstance() -> System.Windows.Forms.AccessibleObject! override System.Windows.Forms.ToolStripSplitButton.CreateDefaultDropDown() -> System.Windows.Forms.ToolStripDropDown! @@ -1462,9 +1462,9 @@ System.Windows.Forms.ToolStripItem.Command.get -> System.Windows.Input.ICommand? System.Windows.Forms.ToolStripItem.Command.set -> void System.Windows.Forms.ToolStripItem.CommandParameter.get -> object? System.Windows.Forms.ToolStripItem.CommandParameter.set -> void -~System.Windows.Forms.ToolStripItem.DoDragDrop(object data, System.Windows.Forms.DragDropEffects allowedEffects) -> System.Windows.Forms.DragDropEffects -~System.Windows.Forms.ToolStripItem.DoDragDrop(object data, System.Windows.Forms.DragDropEffects allowedEffects, System.Drawing.Bitmap dragImage, System.Drawing.Point cursorOffset, bool useDefaultDragImage) -> System.Windows.Forms.DragDropEffects -~System.Windows.Forms.ToolStripItem.GetCurrentParent() -> System.Windows.Forms.ToolStrip +System.Windows.Forms.ToolStripItem.DoDragDrop(object! data, System.Windows.Forms.DragDropEffects allowedEffects) -> System.Windows.Forms.DragDropEffects +System.Windows.Forms.ToolStripItem.DoDragDrop(object! data, System.Windows.Forms.DragDropEffects allowedEffects, System.Drawing.Bitmap? dragImage, System.Drawing.Point cursorOffset, bool useDefaultDragImage) -> System.Windows.Forms.DragDropEffects +System.Windows.Forms.ToolStripItem.GetCurrentParent() -> System.Windows.Forms.ToolStrip? System.Windows.Forms.ToolStripItem.ImageKey.get -> string! System.Windows.Forms.ToolStripItem.ImageKey.set -> void System.Windows.Forms.ToolStripItem.Name.get -> string? @@ -1478,8 +1478,8 @@ System.Windows.Forms.ToolStripItem.Tag.get -> object? System.Windows.Forms.ToolStripItem.Tag.set -> void System.Windows.Forms.ToolStripItem.ToolStripItem(string? text, System.Drawing.Image? image, System.EventHandler? onClick) -> void System.Windows.Forms.ToolStripItem.ToolStripItem(string? text, System.Drawing.Image? image, System.EventHandler? onClick, string? name) -> void -~System.Windows.Forms.ToolStripItem.ToolTipText.get -> string -~System.Windows.Forms.ToolStripItem.ToolTipText.set -> void +System.Windows.Forms.ToolStripItem.ToolTipText.get -> string? +System.Windows.Forms.ToolStripItem.ToolTipText.set -> void ~System.Windows.Forms.ToolStripItemCollection.Add(string text) -> System.Windows.Forms.ToolStripItem ~System.Windows.Forms.ToolStripItemCollection.Add(string text, System.Drawing.Image image) -> System.Windows.Forms.ToolStripItem ~System.Windows.Forms.ToolStripItemCollection.Add(string text, System.Drawing.Image image, System.EventHandler onClick) -> System.Windows.Forms.ToolStripItem @@ -2129,50 +2129,50 @@ virtual System.Windows.Forms.ToolStripDropDownItem.OnDropDownOpened(System.Event virtual System.Windows.Forms.ToolStripDropDownItem.OnDropDownShow(System.EventArgs! e) -> void virtual System.Windows.Forms.ToolStripItem.BackgroundImage.get -> System.Drawing.Image? virtual System.Windows.Forms.ToolStripItem.BackgroundImage.set -> void -~virtual System.Windows.Forms.ToolStripItem.CreateAccessibilityInstance() -> System.Windows.Forms.AccessibleObject +virtual System.Windows.Forms.ToolStripItem.CreateAccessibilityInstance() -> System.Windows.Forms.AccessibleObject! virtual System.Windows.Forms.ToolStripItem.Font.get -> System.Drawing.Font! virtual System.Windows.Forms.ToolStripItem.Font.set -> void virtual System.Windows.Forms.ToolStripItem.Image.get -> System.Drawing.Image? virtual System.Windows.Forms.ToolStripItem.Image.set -> void -~virtual System.Windows.Forms.ToolStripItem.OnAvailableChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnBackColorChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnClick(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnCommandCanExecuteChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnCommandChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnCommandParameterChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnDisplayStyleChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnDoubleClick(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnDragDrop(System.Windows.Forms.DragEventArgs dragEvent) -> void -~virtual System.Windows.Forms.ToolStripItem.OnDragEnter(System.Windows.Forms.DragEventArgs dragEvent) -> void -~virtual System.Windows.Forms.ToolStripItem.OnDragLeave(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnDragOver(System.Windows.Forms.DragEventArgs dragEvent) -> void -~virtual System.Windows.Forms.ToolStripItem.OnEnabledChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnFontChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnForeColorChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnGiveFeedback(System.Windows.Forms.GiveFeedbackEventArgs giveFeedbackEvent) -> void -~virtual System.Windows.Forms.ToolStripItem.OnLayout(System.Windows.Forms.LayoutEventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnLocationChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnMouseDown(System.Windows.Forms.MouseEventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnMouseEnter(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnMouseHover(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnMouseLeave(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnMouseMove(System.Windows.Forms.MouseEventArgs mea) -> void -~virtual System.Windows.Forms.ToolStripItem.OnMouseUp(System.Windows.Forms.MouseEventArgs e) -> void +virtual System.Windows.Forms.ToolStripItem.OnAvailableChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnBackColorChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnClick(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnCommandCanExecuteChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnCommandChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnCommandParameterChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnDisplayStyleChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnDoubleClick(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnDragDrop(System.Windows.Forms.DragEventArgs! dragEvent) -> void +virtual System.Windows.Forms.ToolStripItem.OnDragEnter(System.Windows.Forms.DragEventArgs! dragEvent) -> void +virtual System.Windows.Forms.ToolStripItem.OnDragLeave(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnDragOver(System.Windows.Forms.DragEventArgs! dragEvent) -> void +virtual System.Windows.Forms.ToolStripItem.OnEnabledChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnFontChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnForeColorChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnGiveFeedback(System.Windows.Forms.GiveFeedbackEventArgs! giveFeedbackEvent) -> void +virtual System.Windows.Forms.ToolStripItem.OnLayout(System.Windows.Forms.LayoutEventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnLocationChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnMouseDown(System.Windows.Forms.MouseEventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnMouseEnter(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnMouseHover(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnMouseLeave(System.EventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnMouseMove(System.Windows.Forms.MouseEventArgs! mea) -> void +virtual System.Windows.Forms.ToolStripItem.OnMouseUp(System.Windows.Forms.MouseEventArgs! e) -> void ~virtual System.Windows.Forms.ToolStripItem.OnOwnerChanged(System.EventArgs e) -> void ~virtual System.Windows.Forms.ToolStripItem.OnOwnerFontChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnPaint(System.Windows.Forms.PaintEventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnParentBackColorChanged(System.EventArgs e) -> void +virtual System.Windows.Forms.ToolStripItem.OnPaint(System.Windows.Forms.PaintEventArgs! e) -> void +virtual System.Windows.Forms.ToolStripItem.OnParentBackColorChanged(System.EventArgs! e) -> void ~virtual System.Windows.Forms.ToolStripItem.OnParentChanged(System.Windows.Forms.ToolStrip oldParent, System.Windows.Forms.ToolStrip newParent) -> void ~virtual System.Windows.Forms.ToolStripItem.OnParentEnabledChanged(System.EventArgs e) -> void ~virtual System.Windows.Forms.ToolStripItem.OnParentForeColorChanged(System.EventArgs e) -> void ~virtual System.Windows.Forms.ToolStripItem.OnParentRightToLeftChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.OnQueryContinueDrag(System.Windows.Forms.QueryContinueDragEventArgs queryContinueDragEvent) -> void -~virtual System.Windows.Forms.ToolStripItem.OnRequestCommandExecute(System.EventArgs e) -> void +virtual System.Windows.Forms.ToolStripItem.OnQueryContinueDrag(System.Windows.Forms.QueryContinueDragEventArgs! queryContinueDragEvent) -> void +virtual System.Windows.Forms.ToolStripItem.OnRequestCommandExecute(System.EventArgs! e) -> void ~virtual System.Windows.Forms.ToolStripItem.OnRightToLeftChanged(System.EventArgs e) -> void ~virtual System.Windows.Forms.ToolStripItem.OnTextChanged(System.EventArgs e) -> void ~virtual System.Windows.Forms.ToolStripItem.OnVisibleChanged(System.EventArgs e) -> void -~virtual System.Windows.Forms.ToolStripItem.Text.get -> string -~virtual System.Windows.Forms.ToolStripItem.Text.set -> void +virtual System.Windows.Forms.ToolStripItem.Text.get -> string? +virtual System.Windows.Forms.ToolStripItem.Text.set -> void ~virtual System.Windows.Forms.ToolStripItemCollection.ContainsKey(string key) -> bool ~virtual System.Windows.Forms.ToolStripItemCollection.IndexOfKey(string key) -> int ~virtual System.Windows.Forms.ToolStripItemCollection.RemoveByKey(string key) -> void @@ -11324,7 +11324,7 @@ System.Windows.Forms.ToolStripItem.RightToLeftAutoMirrorImage.get -> bool System.Windows.Forms.ToolStripItem.RightToLeftAutoMirrorImage.set -> void System.Windows.Forms.ToolStripItem.RightToLeftChanged -> System.EventHandler? System.Windows.Forms.ToolStripItem.Select() -> void -System.Windows.Forms.ToolStripItem.TextChanged -> System.EventHandler +System.Windows.Forms.ToolStripItem.TextChanged -> System.EventHandler? System.Windows.Forms.ToolStripItem.TextImageRelation.get -> System.Windows.Forms.TextImageRelation System.Windows.Forms.ToolStripItem.TextImageRelation.set -> void System.Windows.Forms.ToolStripItem.ToolStripItem() -> void @@ -11333,7 +11333,7 @@ System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject.AddState(System System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject.ToolStripItemAccessibleObject(System.Windows.Forms.ToolStripItem! ownerItem) -> void System.Windows.Forms.ToolStripItem.Visible.get -> bool System.Windows.Forms.ToolStripItem.Visible.set -> void -System.Windows.Forms.ToolStripItem.VisibleChanged -> System.EventHandler +System.Windows.Forms.ToolStripItem.VisibleChanged -> System.EventHandler? System.Windows.Forms.ToolStripItem.Width.get -> int System.Windows.Forms.ToolStripItem.Width.set -> void System.Windows.Forms.ToolStripItemAlignment diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripDropDownItem.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripDropDownItem.cs index 3bee0ccab12..6c4012597da 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripDropDownItem.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripDropDownItem.cs @@ -595,7 +595,7 @@ protected internal override bool ProcessDialogKey(Keys keyData) // we're on a drop down but we're heading back up the chain. // remember to select the item that displayed this dropdown. - ToolStripDropDown parent = GetCurrentParentDropDown(); + ToolStripDropDown? parent = GetCurrentParentDropDown(); if (parent is not null && !parent.IsFirstDropDown) { // we're walking back up the dropdown chain. diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.ToolStripItemAccessibleObject.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.ToolStripItemAccessibleObject.cs index 0ef0008577a..23477af2c78 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.ToolStripItemAccessibleObject.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.ToolStripItemAccessibleObject.cs @@ -312,7 +312,7 @@ public override AccessibleObject? Parent if (Owner.IsOnDropDown) { // Return the owner item as the accessible parent. - ToolStripDropDown dropDown = Owner.GetCurrentParentDropDown(); + ToolStripDropDown dropDown = Owner.GetCurrentParentDropDown()!; return dropDown.AccessibilityObject; } 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 2f490c4d380..d1357401452 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs @@ -952,7 +952,7 @@ public virtual Font Font { get { - if (TryGetExplicitlySetFont(out Font font)) + if (TryGetExplicitlySetFont(out Font? font)) { return font; } @@ -1901,7 +1901,7 @@ public object? Tag } set => Properties.SetObject(ToolStripItem.s_tagProperty, value); } -#nullable disable + /// /// The text of the item /// @@ -1909,13 +1909,13 @@ public object? Tag [SRCategory(nameof(SR.CatAppearance))] [Localizable(true)] [SRDescription(nameof(SR.ToolStripItemTextDescr))] - public virtual string Text + public virtual string? Text { get { if (Properties.ContainsObject(ToolStripItem.s_textProperty)) { - return (string)Properties.GetObject(ToolStripItem.s_textProperty); + return (string?)Properties.GetObject(ToolStripItem.s_textProperty); } return string.Empty; @@ -1954,7 +1954,7 @@ public virtual ContentAlignment TextAlign [SRCategory(nameof(SR.CatPropertyChanged))] [SRDescription(nameof(SR.ToolStripItemOnTextChangedDescr))] - public event EventHandler TextChanged + public event EventHandler? TextChanged { add => Events.AddHandler(s_textChangedEvent, value); remove => Events.RemoveHandler(s_textChangedEvent, value); @@ -1969,7 +1969,7 @@ public virtual ToolStripTextDirection TextDirection ToolStripTextDirection textDirection = ToolStripTextDirection.Inherit; if (Properties.ContainsObject(ToolStripItem.s_textDirectionProperty)) { - textDirection = (ToolStripTextDirection)Properties.GetObject(ToolStripItem.s_textDirectionProperty); + textDirection = (ToolStripTextDirection)Properties.GetObject(ToolStripItem.s_textDirectionProperty)!; } if (textDirection == ToolStripTextDirection.Inherit) @@ -2023,17 +2023,17 @@ public TextImageRelation TextImageRelation [SRCategory(nameof(SR.CatBehavior))] [Editor("System.ComponentModel.Design.MultilineStringEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor))] [Localizable(true)] - public string ToolTipText + public string? ToolTipText { get { if (AutoToolTip && string.IsNullOrEmpty(_toolTipText)) { - string toolText = Text; + string? toolText = Text; if (WindowsFormsUtils.ContainsMnemonic(toolText)) { // this shouldn't be called a lot so we can take the perf hit here. - toolText = string.Join("", toolText.Split('&')); + toolText = string.Join(string.Empty, toolText.Split('&')); } return toolText; @@ -2058,7 +2058,7 @@ public bool Visible [SRCategory(nameof(SR.CatPropertyChanged))] [SRDescription(nameof(SR.ToolStripItemOnVisibleChangedDescr))] - public event EventHandler VisibleChanged + public event EventHandler? VisibleChanged { add => Events.AddHandler(s_visibleChangedEvent, value); remove => Events.RemoveHandler(s_visibleChangedEvent, value); @@ -2102,7 +2102,7 @@ private void Animate(bool animate) return; } - Image image = Image; + Image? image = Image; if (image is null) { return; @@ -2124,10 +2124,10 @@ internal bool BeginDragForItemReorder() { if (Control.ModifierKeys == Keys.Alt) { - if (ParentInternal.Items.Contains(this) && ParentInternal.AllowItemReorder) + if (ParentInternal is not null && ParentInternal.Items.Contains(this) && ParentInternal.AllowItemReorder) { // we only drag - ToolStripItem item = this as ToolStripItem; + ToolStripItem item = this; DoDragDrop(item, DragDropEffects.Move); return true; } @@ -2227,14 +2227,13 @@ public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects) /// /// [EditorBrowsable(EditorBrowsableState.Advanced)] - public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, Bitmap dragImage, Point cursorOffset, bool useDefaultDragImage) + public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, Bitmap? dragImage, Point cursorOffset, bool useDefaultDragImage) { - IComDataObject dataObject = null; + IComDataObject? dataObject = data as IComDataObject; - dataObject = data as IComDataObject; if (dataObject is null) { - DataObject iwdata = null; + DataObject? iwdata = null; if (data is IDataObject idataObject) { iwdata = new DataObject(idataObject); @@ -2289,7 +2288,7 @@ public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, B /// Else if the parent does not support reordering of items (Parent.AllowItemReorder = false) - /// then call back on the ToolStripItem's OnQueryContinueDrag/OnGiveFeedback methods. /// - internal Ole32.IDropSource CreateDropSource(IComDataObject dataObject, Bitmap dragImage, Point cursorOffset, bool useDefaultDragImage) + internal Ole32.IDropSource CreateDropSource(IComDataObject dataObject, Bitmap? dragImage, Point cursorOffset, bool useDefaultDragImage) { if (ParentInternal is not null && ParentInternal.AllowItemReorder && ParentInternal.ItemReorderDropSource is not null) { @@ -2309,7 +2308,7 @@ internal void FireEvent(EventArgs e, ToolStripItemEventType met) OnLocationChanged(e); break; case ToolStripItemEventType.Paint: - HandlePaint(e as PaintEventArgs); + HandlePaint((PaintEventArgs)e); break; case ToolStripItemEventType.MouseHover: // disabled toolstrip items should show tooltips. @@ -2368,16 +2367,16 @@ internal void FireEventInteractive(EventArgs e, ToolStripItemEventType met) switch (met) { case ToolStripItemEventType.MouseMove: - HandleMouseMove(e as MouseEventArgs); + HandleMouseMove((MouseEventArgs)e); break; case ToolStripItemEventType.MouseHover: - HandleMouseHover(e as EventArgs); + HandleMouseHover(e); break; case ToolStripItemEventType.MouseUp: - HandleMouseUp(e as MouseEventArgs); + HandleMouseUp((MouseEventArgs)e); break; case ToolStripItemEventType.MouseDown: - HandleMouseDown(e as MouseEventArgs); + HandleMouseDown((MouseEventArgs)e); break; case ToolStripItemEventType.Click: HandleClick(e); @@ -2392,15 +2391,15 @@ internal void FireEventInteractive(EventArgs e, ToolStripItemEventType met) } } - private Font GetOwnerFont() => Owner?.Font; + private Font? GetOwnerFont() => Owner?.Font; /// /// We don't want a public settable property and usually owner will work /// except for things like the overflow button /// - public ToolStrip GetCurrentParent() => Parent; + public ToolStrip? GetCurrentParent() => Parent; - internal ToolStripDropDown GetCurrentParentDropDown() + internal ToolStripDropDown? GetCurrentParentDropDown() { if (ParentInternal is not null) { @@ -2645,7 +2644,7 @@ private void HandleMouseUp(MouseEventArgs e) { s_mouseDebugging.TraceVerbose($"[{Text}] MouseUp"); - bool fireMouseUp = (ParentInternal.LastMouseDownedItem == this); + bool fireMouseUp = (ParentInternal?.LastMouseDownedItem == this); if (!fireMouseUp && !MouseDownAndUpMustBeInSameItem) { @@ -2654,7 +2653,7 @@ private void HandleMouseUp(MouseEventArgs e) // that the mouse has actually moved from when a dropdown has been opened - // otherwise we may accidentally click what's underneath the mouse at the time // the dropdown is opened. - fireMouseUp = ParentInternal.ShouldSelectItem(); + fireMouseUp = ParentInternal is not null && ParentInternal.ShouldSelectItem(); } if (_state[s_stateMouseDownAndNoDrag] || fireMouseUp) @@ -2750,9 +2749,9 @@ void ISupportOleDropSource.OnGiveFeedback(GiveFeedbackEventArgs giveFeedbackEven void ISupportOleDropSource.OnQueryContinueDrag(QueryContinueDragEventArgs queryContinueDragEventArgs) => OnQueryContinueDrag(queryContinueDragEventArgs); - private void OnAnimationFrameChanged(object o, EventArgs e) + private void OnAnimationFrameChanged(object? o, EventArgs e) { - ToolStrip parent = ParentInternal; + ToolStrip? parent = ParentInternal; if (parent is not null) { if (parent.Disposing || parent.IsDisposed) @@ -2762,7 +2761,7 @@ private void OnAnimationFrameChanged(object o, EventArgs e) if (parent.IsHandleCreated && parent.InvokeRequired) { - parent.BeginInvoke(new EventHandler(OnAnimationFrameChanged), new object[] { o, e }); + parent.BeginInvoke(new EventHandler(OnAnimationFrameChanged), new object?[] { o, e }); return; } @@ -2788,7 +2787,7 @@ protected virtual void OnCommandChanged(EventArgs e) [RequiresPreviewFeatures] [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnCommandCanExecuteChanged(EventArgs e) - => ((EventHandler)Events[s_commandCanExecuteChangedEvent])?.Invoke(this, e); + => ((EventHandler?)Events[s_commandCanExecuteChangedEvent])?.Invoke(this, e); /// /// Raises the event. @@ -2866,7 +2865,7 @@ protected virtual void OnDisplayStyleChanged(EventArgs e) /// [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnGiveFeedback(GiveFeedbackEventArgs giveFeedbackEvent) - => ((GiveFeedbackEventHandler)Events[s_giveFeedbackEvent])?.Invoke(this, giveFeedbackEvent); + => ((GiveFeedbackEventHandler?)Events[s_giveFeedbackEvent])?.Invoke(this, giveFeedbackEvent); internal virtual void OnImageScalingSizeChanged(EventArgs e) { @@ -2982,7 +2981,7 @@ protected virtual void OnParentBackColorChanged(EventArgs e) OnBackColorChanged(e); } } - +#nullable disable /// /// Inheriting classes should override this method to handle this event. /// diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripMenuItem.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripMenuItem.cs index af4e0d89622..84bb81a2e78 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripMenuItem.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripMenuItem.cs @@ -988,7 +988,7 @@ private void OnMouseButtonStateChange(MouseEventArgs e, bool isMouseDown) bool showDropDown = true; if (IsOnDropDown) { - ToolStripDropDown dropDown = GetCurrentParentDropDown() as ToolStripDropDown; + ToolStripDropDown dropDown = GetCurrentParentDropDown()!; // Right click support for context menus. // used in ToolStripItem to determine whether to fire click OnMouseUp. diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripSeparator.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripSeparator.cs index d0bed42409d..6427e6241fe 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripSeparator.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripSeparator.cs @@ -198,8 +198,7 @@ private bool IsVertical [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] - [AllowNull] - public override string Text + public override string? Text { get => base.Text; set => base.Text = value; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripSystemRenderer.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripSystemRenderer.cs index 7ffc77ec724..06ad75670c0 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripSystemRenderer.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripSystemRenderer.cs @@ -666,7 +666,7 @@ private static void RenderItemInternal(ToolStripItemRenderEventArgs e) } else { - ToolStrip parent = item.GetCurrentParent(); + ToolStrip? parent = item.GetCurrentParent(); if ((parent is not null) && (state != ToolBarState.Checked) && (item.BackColor != parent.BackColor)) { FillBackground(g, fillRect, item.BackColor); 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 b379ce1e040..dcfe70cd587 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.Globalization; using System.Text; @@ -38,7 +39,7 @@ public static Point LastCursorPoint /// this is your function. If you have a character "t" and want match it to &Text /// Control.IsMnemonic is a better bet. /// - public static bool ContainsMnemonic(string? text) + public static bool ContainsMnemonic([NotNullWhen(true)] string? text) { if (text is not null) {