Skip to content

Commit 6ef08d7

Browse files
authored
Enable nullability in classes that inherit from DesignerActionList (#9084)
* Enable nullability in classes that inherit from DesignerActionList * Check if Component is null * Use fields to avoid using null-forgiving operator in Component
1 parent 4caf88b commit 6ef08d7

11 files changed

+84
-78
lines changed

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ContextMenuStripActionList.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.ComponentModel;
86
using System.ComponentModel.Design;
97

@@ -14,15 +12,16 @@ internal class ContextMenuStripActionList : DesignerActionList
1412
private readonly ToolStripDropDown _toolStripDropDown;
1513
private bool _autoShow;
1614

17-
public ContextMenuStripActionList(ToolStripDropDownDesigner designer) : base(designer.Component)
15+
public ContextMenuStripActionList(ToolStripDropDownDesigner designer)
16+
: base(designer.Component)
1817
{
1918
_toolStripDropDown = (ToolStripDropDown)designer.Component;
2019
}
2120

2221
//helper function to get the property on the actual Control
23-
private object GetProperty(string propertyName)
22+
private object? GetProperty(string propertyName)
2423
{
25-
PropertyDescriptor getProperty = TypeDescriptor.GetProperties(_toolStripDropDown)[propertyName];
24+
PropertyDescriptor? getProperty = TypeDescriptor.GetProperties(_toolStripDropDown)[propertyName];
2625
Debug.Assert(getProperty is not null, "Could not find given property in control.");
2726
if (getProperty is not null)
2827
{
@@ -35,7 +34,7 @@ private object GetProperty(string propertyName)
3534
//helper function to change the property on the actual Control
3635
private void ChangeProperty(string propertyName, object value)
3736
{
38-
PropertyDescriptor changingProperty = TypeDescriptor.GetProperties(_toolStripDropDown)[propertyName];
37+
PropertyDescriptor? changingProperty = TypeDescriptor.GetProperties(_toolStripDropDown)[propertyName];
3938
Debug.Assert(changingProperty is not null, "Could not find given property in control.");
4039
changingProperty?.SetValue(_toolStripDropDown, value);
4140
}
@@ -57,7 +56,7 @@ public override bool AutoShow
5756

5857
public bool ShowImageMargin
5958
{
60-
get => (bool)GetProperty(nameof(ShowImageMargin));
59+
get => (bool)GetProperty(nameof(ShowImageMargin))!;
6160
set
6261
{
6362
if (value != ShowImageMargin)
@@ -69,7 +68,7 @@ public bool ShowImageMargin
6968

7069
public bool ShowCheckMargin
7170
{
72-
get => (bool)GetProperty(nameof(ShowCheckMargin));
71+
get => (bool)GetProperty(nameof(ShowCheckMargin))!;
7372
set
7473
{
7574
if (value != ShowCheckMargin)
@@ -81,7 +80,7 @@ public bool ShowCheckMargin
8180

8281
public ToolStripRenderMode RenderMode
8382
{
84-
get => (ToolStripRenderMode)GetProperty(nameof(RenderMode));
83+
get => (ToolStripRenderMode)GetProperty(nameof(RenderMode))!;
8584
set
8685
{
8786
if (value != RenderMode)

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ControlDesigner.DockingActionList.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.ComponentModel;
86
using System.ComponentModel.Design;
97

@@ -13,21 +11,25 @@ public partial class ControlDesigner
1311
{
1412
private class DockingActionList : DesignerActionList
1513
{
16-
private readonly ControlDesigner _designer;
17-
private readonly IDesignerHost _host;
14+
private readonly IDesignerHost? _host;
1815

19-
public DockingActionList(ControlDesigner owner) : base(owner.Component)
16+
public DockingActionList(ControlDesigner owner)
17+
: base(owner.Component)
2018
{
21-
_designer = owner;
2219
_host = GetService(typeof(IDesignerHost)) as IDesignerHost;
2320
}
2421

25-
private string GetActionName()
22+
private string? GetActionName()
2623
{
27-
PropertyDescriptor dockProp = TypeDescriptor.GetProperties(Component)["Dock"];
24+
if (Component is null)
25+
{
26+
return null;
27+
}
28+
29+
PropertyDescriptor? dockProp = TypeDescriptor.GetProperties(Component)["Dock"];
2830
if (dockProp is not null)
2931
{
30-
DockStyle dockStyle = (DockStyle)dockProp.GetValue(Component);
32+
DockStyle dockStyle = (DockStyle)dockProp.GetValue(Component)!;
3133
if (dockStyle == DockStyle.Fill)
3234
{
3335
return SR.DesignerShortcutUndockInParent;
@@ -44,24 +46,24 @@ private string GetActionName()
4446
public override DesignerActionItemCollection GetSortedActionItems()
4547
{
4648
DesignerActionItemCollection items = new DesignerActionItemCollection();
47-
string actionName = GetActionName();
49+
string? actionName = GetActionName();
4850
if (actionName is not null)
4951
{
50-
items.Add(new DesignerActionVerbItem(new DesignerVerb(GetActionName(), OnDockActionClick)));
52+
items.Add(new DesignerActionVerbItem(new DesignerVerb(actionName, OnDockActionClick)));
5153
}
5254

5355
return items;
5456
}
5557

56-
private void OnDockActionClick(object sender, EventArgs e)
58+
private void OnDockActionClick(object? sender, EventArgs e)
5759
{
5860
if (sender is DesignerVerb designerVerb && _host is not null)
5961
{
6062
using DesignerTransaction t = _host.CreateTransaction(designerVerb.Text);
6163

6264
// Set the dock prop to DockStyle.Fill
63-
PropertyDescriptor dockProp = TypeDescriptor.GetProperties(Component)["Dock"];
64-
DockStyle dockStyle = (DockStyle)dockProp.GetValue(Component);
65+
PropertyDescriptor dockProp = TypeDescriptor.GetProperties(Component!)["Dock"]!;
66+
DockStyle dockStyle = (DockStyle)dockProp.GetValue(Component)!;
6567
dockProp.SetValue(Component, dockStyle == DockStyle.Fill ? DockStyle.None : DockStyle.Fill);
6668
t.Commit();
6769
}

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbList.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ internal class DesignerActionVerbList : DesignerActionList
88
{
99
private readonly DesignerVerb[] _verbs;
1010

11-
public DesignerActionVerbList(DesignerVerb[] verbs) : base(null)
11+
public DesignerActionVerbList(DesignerVerb[] verbs)
12+
: base(null)
1213
{
1314
_verbs = verbs;
1415
}

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ListControlUnboundActionList.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ internal class ListControlUnboundActionList : DesignerActionList
1010
{
1111
private readonly ComponentDesigner _designer;
1212

13-
public ListControlUnboundActionList(ComponentDesigner designer) : base(designer.Component)
13+
public ListControlUnboundActionList(ComponentDesigner designer)
14+
: base(designer.Component)
1415
{
1516
_designer = designer;
1617
}

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ListViewActionList.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.ComponentModel.Design;
86
using System.ComponentModel;
97

@@ -12,9 +10,13 @@ namespace System.Windows.Forms.Design;
1210
internal class ListViewActionList : DesignerActionList
1311
{
1412
private readonly ComponentDesigner _designer;
15-
public ListViewActionList(ComponentDesigner designer) : base(designer.Component)
13+
private readonly ListView _listView;
14+
15+
public ListViewActionList(ComponentDesigner designer)
16+
: base(designer.Component)
1617
{
1718
_designer = designer;
19+
_listView = (ListView)Component!;
1820
}
1921

2022
public void InvokeItemsDialog()
@@ -36,35 +38,35 @@ public View View
3638
{
3739
get
3840
{
39-
return ((ListView)Component).View;
41+
return _listView.View;
4042
}
4143
set
4244
{
43-
TypeDescriptor.GetProperties(Component)["View"].SetValue(Component, value);
45+
TypeDescriptor.GetProperties(_listView)["View"]!.SetValue(Component, value);
4446
}
4547
}
4648

47-
public ImageList LargeImageList
49+
public ImageList? LargeImageList
4850
{
4951
get
5052
{
51-
return ((ListView)Component).LargeImageList;
53+
return _listView.LargeImageList;
5254
}
5355
set
5456
{
55-
TypeDescriptor.GetProperties(Component)["LargeImageList"].SetValue(Component, value);
57+
TypeDescriptor.GetProperties(_listView)["LargeImageList"]!.SetValue(Component, value);
5658
}
5759
}
5860

59-
public ImageList SmallImageList
61+
public ImageList? SmallImageList
6062
{
6163
get
6264
{
63-
return ((ListView)Component).SmallImageList;
65+
return _listView.SmallImageList;
6466
}
6567
set
6668
{
67-
TypeDescriptor.GetProperties(Component)["SmallImageList"].SetValue(Component, value);
69+
TypeDescriptor.GetProperties(_listView)["SmallImageList"]!.SetValue(Component, value);
6870
}
6971
}
7072

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/MaskedTextBoxDesignerActionList.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.ComponentModel;
86
using System.ComponentModel.Design;
97

@@ -16,16 +14,17 @@ namespace System.Windows.Forms.Design;
1614
internal class MaskedTextBoxDesignerActionList : DesignerActionList
1715
{
1816
private readonly MaskedTextBox _maskedTextBox;
19-
private readonly ITypeDiscoveryService _discoverySvc;
20-
private readonly IUIService _uiSvc;
21-
private readonly IHelpService _helpService;
17+
private readonly ITypeDiscoveryService? _discoverySvc;
18+
private readonly IUIService? _uiSvc;
19+
private readonly IHelpService? _helpService;
2220

2321
/// <summary>
2422
/// Constructor receiving a MaskedTextBox control the action list applies to. The ITypeDiscoveryService
2523
/// service provider is used to populate the canned mask list control in the MaskDesignerDialog dialog and
2624
/// the IUIService provider is used to display the MaskDesignerDialog within VS.
2725
/// </summary>
28-
public MaskedTextBoxDesignerActionList(MaskedTextBoxDesigner designer) : base(designer.Component)
26+
public MaskedTextBoxDesignerActionList(MaskedTextBoxDesigner designer)
27+
: base(designer.Component)
2928
{
3029
_maskedTextBox = (MaskedTextBox)designer.Component;
3130
_discoverySvc = GetService(typeof(ITypeDiscoveryService)) as ITypeDiscoveryService;
@@ -50,7 +49,7 @@ public void SetMask()
5049
return;
5150
}
5251

53-
PropertyDescriptor maskProperty = TypeDescriptor.GetProperties(_maskedTextBox)["Mask"];
52+
PropertyDescriptor? maskProperty = TypeDescriptor.GetProperties(_maskedTextBox)["Mask"];
5453
maskProperty?.SetValue(_maskedTextBox, mask);
5554
}
5655

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/PictureBoxActionList.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.ComponentModel.Design;
86
using System.ComponentModel;
97

@@ -12,20 +10,24 @@ namespace System.Windows.Forms.Design;
1210
internal class PictureBoxActionList : DesignerActionList
1311
{
1412
private readonly PictureBoxDesigner _designer;
15-
public PictureBoxActionList(PictureBoxDesigner designer) : base(designer.Component)
13+
private readonly PictureBox _pictureBox;
14+
15+
public PictureBoxActionList(PictureBoxDesigner designer)
16+
: base(designer.Component)
1617
{
1718
_designer = designer;
19+
_pictureBox = (PictureBox)designer.Component;
1820
}
1921

2022
public PictureBoxSizeMode SizeMode
2123
{
2224
get
2325
{
24-
return ((PictureBox)Component).SizeMode;
26+
return _pictureBox.SizeMode;
2527
}
2628
set
2729
{
28-
TypeDescriptor.GetProperties(Component)["SizeMode"].SetValue(Component, value);
30+
TypeDescriptor.GetProperties(_pictureBox)["SizeMode"]!.SetValue(Component, value);
2931
}
3032
}
3133

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/RichTextBoxActionList.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ internal class RichTextBoxActionList : DesignerActionList
1010
{
1111
private readonly RichTextBoxDesigner _designer;
1212

13-
public RichTextBoxActionList(RichTextBoxDesigner designer) : base(designer.Component)
13+
public RichTextBoxActionList(RichTextBoxDesigner designer)
14+
: base(designer.Component)
1415
{
1516
_designer = designer;
1617
}

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/TextBoxActionList.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.ComponentModel;
86
using System.ComponentModel.Design;
97

108
namespace System.Windows.Forms.Design;
119

1210
internal class TextBoxActionList : DesignerActionList
1311
{
14-
public TextBoxActionList(TextBoxDesigner designer) : base(designer.Component)
12+
private readonly TextBox _textBox;
13+
14+
public TextBoxActionList(TextBoxDesigner designer)
15+
: base(designer.Component)
1516
{
17+
_textBox = (TextBox)designer.Component;
1618
}
1719

1820
public bool Multiline
1921
{
2022
get
2123
{
22-
return ((TextBox)Component).Multiline;
24+
return _textBox.Multiline;
2325
}
2426
set
2527
{
26-
TypeDescriptor.GetProperties(Component)["Multiline"].SetValue(Component, value);
28+
TypeDescriptor.GetProperties(_textBox)["Multiline"]!.SetValue(Component, value);
2729
}
2830
}
2931

0 commit comments

Comments
 (0)