dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.4k stars 977 forks source link

Enable nullability across winforms #1107

Open hughbe opened 5 years ago

hughbe commented 5 years ago

Once CoreFx and CoreCLR fully support nullability it would be great to do this in winforms

Probably a large project...

merriemcgaw commented 5 years ago

Got a few things on the priority list, but we can consider adding this to the roadmap. @OliaG for Roadmap thoughts.

Logerfo commented 4 years ago

Implementing NRT to the whole code base would be a massive work, but enabling it only for the API surface would be much easier and yet very helpful for customers who uses both winforms and NRT.

drewnoakes commented 4 years ago

Something we did on project system, CPS and other projects is to add <Nullable>enable</Nullable> to project files, then use a regular expression to add #nullable disable to each and every source file.

This encourages new code to be written using NRT, and provides an annotation to-do list by searching for #nullable disable.

For commonly used types where annotations would be useful, but it's too challenging to fix all generated warnings, it's possible to use #nullable annotations which allows adding ? to types without having to address any warnings.

RussKie commented 4 years ago

Thank you @drewnoakes, this may be a way forward.

Logerfo commented 4 years ago
#nullable annotations

@drewnoakes where is this documented? VS syntax highlight it, but the compiler is bothered:

CS8637 Expected 'enable', 'disable', or 'restore'

Edit: Oh, nevermind, got it. It's #nullable enable annotations.

sharwell commented 4 years ago

I've done this for a few projects to date, and may be able to help here.

Logerfo commented 4 years ago

Now that #2702 and #2756 are merged, I encourage everyone interested to send PRs covering small parts of the API for NRT in SWF.

RussKie commented 4 years ago

When annotating Windows Forms types verify that the Runtime types are NRT enabled (refer to https://github.com/dotnet/runtime/issues/2339).

elachlan commented 1 year ago

List of files in System.Windows.Forms which need annotation, ordered by line count ascending.

Powershell code used:

Get-ChildItem -re -in "*.cs" |
Foreach-Object { 
    $fileStats = Get-Content $_.FullName | Measure-Object -line
    $word='#nullable disable'
    $found = Select-String -Path $_ -Pattern $word -SimpleMatch -Quiet
    $linesInFile = $fileStats.Lines
    Write-Host "$_,$linesInFile,$found" 
} 
elachlan commented 1 year ago

List of files in System.Windows.Forms which need annotation, ordered by line count ascending.

\src\System\Drawing\Design\IPropertyValueUIService.cs   39
\src\System\Windows\Forms\DataGridViewCellLinkedListElement.cs  39
\src\System\Windows\Forms\ListManagerBindingsCollection.cs  59
\src\System\Windows\Forms\MDIClient.ControlCollection.cs    65
\src\System\Windows\Forms\Design\IUIService.cs  70
\src\System\Windows\Forms\HtmlWindow.HTMLWindowEvents2.cs   74
\src\System\Windows\Forms\Layout\TableLayout.LayoutInfo.cs  75
\src\System\Windows\Forms\DataGridViewSelectedRowCollection.cs  76
\src\System\Windows\Forms\Layout\FlowLayout.ElementProxy.cs 77
\src\System\Windows\Forms\DataGridViewSelectedColumnCollection.cs   78
\src\System\Windows\Forms\TabControl.ControlCollection.cs   79
\src\System\Windows\Forms\PropertyGridInternal\MergePropertyDescriptor.MultiMergeCollection.cs  80
\src\System\Windows\Forms\DataGridViewTextBoxColumn.cs  91
\src\System\Windows\Forms\PropertyGridInternal\ImmutablePropertyDescriptorGridEntry.cs  93
\src\System\Windows\Forms\RelatedPropertyManager.cs 93
\src\System\Windows\Forms\HtmlWindowCollection.cs   97
\src\System\Windows\Forms\BindingsCollection.cs 102
\src\System\Windows\Forms\DataGridViewSelectedCellCollection.cs 105
\src\System\Windows\Forms\HtmlWindow.HtmlWindowShim.cs  105
\src\System\Windows\Forms\HtmlElementEventArgs.cs   106
\src\System\Windows\Forms\DataGridView.HitTestInfo.cs   112
\src\System\Windows\Forms\DataGridViewRowCollection.RowArrayList.cs 116
\src\System\Windows\Forms\HtmlDocument.HtmlDocumentShim.cs  119
\src\System\Windows\Forms\HtmlToClrEventProxy.cs    131
\src\System\Windows\Forms\DataGridViewRowCollection.RowComparer.cs  133
\src\System\Windows\Forms\HtmlElement.HtmlElementShim.cs    138
\src\System\Windows\Forms\AutoCompleteStringCollection.cs   144
\src\System\Windows\Forms\HtmlShim.cs   145
\src\System\Windows\Forms\TableLayoutSettings.TableLayoutSettingsStub.cs    146
\src\System\Windows\Forms\ControlBindingsCollection.cs  156
\src\System\Windows\Forms\DataGridViewComboBoxEditingControl.cs 158
\src\System\Windows\Forms\HtmlDocument.HTMLDocumentEvents2.cs   158
\src\System\Windows\Forms\DataGridViewCellPaintingEventArgs.cs  162
\src\System\Windows\Forms\MDIControlStrip.cs    168
\src\System\Windows\Forms\ToolStripPanel.ToolStripPanelRowCollection.cs 168
\src\System\Windows\Forms\HtmlElementCollection.cs  169
\src\System\Windows\Forms\WebBrowser.WebBrowserSite.cs  171
\src\System\Windows\Forms\DataGridViewCellLinkedList.cs 175
\src\System\Windows\Forms\RelatedCurrencyManager.cs 180
\src\System\Windows\Forms\DataGridViewIntLinkedList.cs  183
\src\System\Windows\Forms\WebBrowser.WebBrowserEvent.cs 188
\src\System\Windows\Forms\PropertyManager.cs    189
\src\System\Windows\Forms\DataGridViewButtonColumn.cs   209
\src\System\Windows\Forms\HtmlShimManager.cs    228
\src\System\Windows\Forms\PropertyGridInternal\MultiSelectRootGridEntry.PropertyMerger.cs   254
\src\System\Windows\Forms\DataGridViewComboBoxCell.ObjectCollection.cs  259
\src\System\Windows\Forms\ImageListStreamer.cs  261
\src\System\Windows\Forms\ToolStripPanelRow.ToolStripPanelRowControlCollection.cs   261
\src\System\Windows\Forms\DataGridViewImageColumn.cs    287
\src\System\Windows\Forms\PropertyGridInternal\MergePropertyDescriptor.cs   297
\src\System\Windows\Forms\HtmlElement.HTMLElementEvents2.cs 299
\src\System\Windows\Forms\DataGridViewCheckBoxColumn.cs 300
\src\System\Windows\Forms\DataGridViewCellCollection.cs 310
\src\System\Windows\Forms\Layout\FlowLayout.cs  330
\src\System\Windows\Forms\DataGridViewLinkColumn.cs 336
\src\System\Windows\Forms\AxHost.AxPropertyDescriptor.cs    338
\src\System\Windows\Forms\HtmlWindow.cs 343
\src\System\Windows\Forms\DataGridViewTopLeftHeaderCell.cs  350
\src\System\Windows\Forms\WebBrowserSiteBase.cs 370
\src\System\Windows\Forms\ListView.ListViewNativeItemCollection.cs  392
\src\System\Windows\Forms\ComboBox.ObjectCollection.cs  421
\src\System\Windows\Forms\ListView.ListViewItemCollection.cs    436
\src\System\Windows\Forms\ToolStripItemCollection.cs    448
\src\System\Windows\Forms\DataGridViewComboBoxColumn.cs 478
\src\System\Windows\Forms\HtmlDocument.cs   557
\src\System\Windows\Forms\DataGridViewHeaderCell.cs 574
\src\System\Windows\Forms\HtmlElement.cs    600
\src\System\Windows\Forms\TreeNodeCollection.cs 616
\src\System\Windows\Forms\ListBindingHelper.cs  618
\src\System\Windows\Forms\ToolStripSplitStackLayout.cs  635
\src\System\Windows\Forms\ToolStripPanelRow.cs  655
\src\System\Windows\Forms\DataGridViewTextBoxCell.cs    703
\src\System\Windows\Forms\PropertyGridInternal\PropertyDescriptorGridEntry.cs   762
\src\System\Windows\Forms\ToolStripControlHost.cs   763
\src\System\Windows\Forms\DataGridViewCellStyle.cs  764
\src\System\Windows\Forms\DataGridViewImageCell.cs  834
\src\System\Windows\Forms\DataGridViewButtonCell.cs 940
\src\System\Windows\Forms\CurrencyManager.cs    998
\src\System\Windows\Forms\DataGridViewColumn.cs 1011
\src\System\Windows\Forms\DataGridViewRowHeaderCell.cs  1014
\src\System\Windows\Forms\DataGridViewLinkCell.cs   1021
\src\System\Windows\Forms\DataGridViewColumnCollection.cs   1053
\src\System\Windows\Forms\Layout\DefaultLayout.cs   1063
\src\System\Windows\Forms\PictureBox.cs 1083
\src\System\Windows\Forms\DataGridViewColumnHeaderCell.cs   1105
\src\System\Windows\Forms\Binding.cs    1151
\src\System\Windows\Forms\ListViewItem.cs   1163
\src\System\Windows\Forms\WebBrowser.cs 1307
\src\System\Windows\Forms\ToolStripManager.cs   1317
\src\System\Windows\Forms\Layout\TableLayout.cs 1330
\src\System\Windows\Forms\DataGridView.DataConnection.cs    1475
\src\System\Windows\Forms\WebBrowserBase.cs 1584
\src\System\Windows\Forms\DataGridViewCheckBoxCell.cs   1591
\src\System\Windows\Forms\BindingSource.cs  1597
\src\System\Windows\Forms\DataGridViewRow.cs    1602
\src\System\Windows\Forms\PropertyGridInternal\GridEntry.cs 1909
\src\System\Windows\Forms\TabControl.cs 1949
\src\System\Windows\Forms\TreeNode.cs   2010
\src\System\Windows\Forms\ToolTip.cs    2075
\src\System\Windows\Forms\DataGridViewRowCollection.cs  2206
\src\System\Windows\Forms\DataGridViewComboBoxCell.cs   2320
\src\System\Windows\Forms\TreeView.cs   3158
\src\System\Windows\Forms\RichTextBox.cs    3201
\src\System\Windows\Forms\ComboBox.cs   3541
\src\System\Windows\Forms\AxHost.cs 3615
\src\System\Windows\Forms\DataGridViewCell.cs   3653
\src\System\Windows\Forms\PropertyGrid.cs   3780
\src\System\Windows\Forms\ToolStrip.cs  4362
\src\System\Windows\Forms\DataGridView.cs   4654
\src\System\Windows\Forms\PropertyGridInternal\PropertyGridView.cs  4669
\src\System\Windows\Forms\Control.cs    11967
\src\System\Windows\Forms\DataGridView.Methods.cs   27098
elachlan commented 1 year ago

@gpetrou I am maintaining a list of files that need annotating here. I have been working down the list.

gpetrou commented 1 year ago

@elachlan please note that I have pending PRs for some of these files and that some of the rest are partially annotated. Also, as I have mentioned before, I am not planning to do any work on DataGridView-related files.

merriemcgaw commented 1 year ago

We've made great progress in .NET 8 all, thank you so much!! I'm moving this issue to .NET 9 and we'll keep going with more next release.

elachlan commented 1 year ago

Remaining files that need annotation

File | Lines Of Code -- | -- src\System\Windows\Forms\Binding.cs | 1031 src\System\Windows\Forms\BindingSource.cs | 1592 src\System\Windows\Forms\CurrencyManager.cs | 954 src\System\Windows\Forms\DataGridView.DataConnection.cs | 1440 src\System\Windows\Forms\DataGridView.Methods.cs | 27195 src\System\Windows\Forms\DataGridViewCell.cs | 3633 src\System\Windows\Forms\DataGridViewCheckBoxCell.cs | 1602 src\System\Windows\Forms\DataGridViewCheckBoxColumn.cs | 288 src\System\Windows\Forms\DataGridViewColumnCollection.cs | 1049 src\System\Windows\Forms\DataGridViewColumnHeaderCell.cs | 1080 src\System\Windows\Forms\DataGridViewComboBoxCell.cs | 2292 src\System\Windows\Forms\DataGridViewComboBoxColumn.cs | 466 src\System\Windows\Forms\DataGridViewHeaderCell.cs | 545 src\System\Windows\Forms\DataGridViewImageCell.cs | 829 src\System\Windows\Forms\DataGridViewIntLinkedList.cs | 179 src\System\Windows\Forms\DataGridViewLinkColumn.cs | 324 src\System\Windows\Forms\DataGridViewRowHeaderCell.cs | 1010 src\System\Windows\Forms\PropertyGrid.cs | 3776 src\System\Windows\Forms\PropertyGridInternal\GridEntry.cs | 1902 src\System\Windows\Forms\PropertyGridInternal\MultiSelectRootGridEntry.PropertyMerger.cs | 251 src\System\Windows\Forms\PropertyGridInternal\PropertyDescriptorGridEntry.cs | 759 src\System\Windows\Forms\PropertyGridInternal\PropertyGridView.cs | 4667 src\System\Windows\Forms\TreeNode.cs | 1869
lonitra commented 2 months ago

We've completely enabled nullability for System.Windows.Forms assembly, which is amazing! Thank you all so much 🥳 I will move this over to .NET 10 and we will keep going with the other assemblies e.g. https://github.com/dotnet/winforms/issues/8342