This is what will work with a VB .NET project set to use .NET 5 without adding any references to the project.
Full source
- Front end project source code.
- Back end project support source code.
Language extension
Imports System.Reflection
Imports System.Runtime.CompilerServices
Public Module ListViewExtensions
<Extension>
Public Function GetSelectedItem(listView1 As ListView) As ListViewItem
Return (If(listView1.SelectedItems.Count > 0, listView1.SelectedItems(0), Nothing))
End Function
End Module
Example adding a new item
Private Async Sub OnTraverseEvent(information As DirectoryItem)
Await Task.Delay(100)
FoldersListView.InvokeIfRequired(Sub(listView)
Dim item = New ListViewItem(information.ItemArray)
item.Tag = information
listView.Items.Add(item)
End Sub)
ProcessingLabel.InvokeIfRequired(Sub(label)
label.Text = $"{FoldersListView.Items.Count}"
End Sub)
End Sub
Get current selection, here from a context menu
Private Sub OpenFolderContextMenuStrip_Opening(sender As Object, e As ComponentModel.CancelEventArgs) _
Handles OpenFolderContextMenuStrip.Opening
If FoldersListView.Items.Count = 0 Then
e.Cancel = True
Else
Dim selected = FoldersListView.GetSelectedItem()
End If
End Sub
Project file (note references)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<RootNamespace>RecurseFolders</RootNamespace>
<StartupObject>Sub Main</StartupObject>
<UseWindowsForms>true</UseWindowsForms>
<MyType>WindowsForms</MyType>
<OptionStrict>On</OptionStrict>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<WarningsAsErrors>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<WarningsAsErrors>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Import Include="System.Data" />
<Import Include="System.Drawing" />
<Import Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FileHelpers\FileHelpers.vbproj" />
<ProjectReference Include="..\FolderBrowserDialog\FolderBrowserDialog.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="My Project\Application.Designer.vb">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Application.myapp</DependentUpon>
</Compile>
<Compile Update="My Project\Resources.Designer.vb">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="My Project\Resources.resx">
<CustomToolNamespace>My.Resources</CustomToolNamespace>
<Generator>VbMyResourcesResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.vb</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="My Project\Application.myapp">
<Generator>MyApplicationCodeGenerator</Generator>
<LastGenOutput>Application.Designer.vb</LastGenOutput>
</None>
</ItemGroup>
</Project>