ListViewItem in Vb.Net .Net 5

MRUTYUNJAYA MAHAPATRA 1 Reputation point
2021-04-19T08:25:32.163+00:00

I am migrating one Vb.Net Project into .Net 5.

Dim lvi As ListViewItem = ListView1.SelectedItems(0)

Error :

error BC30002: Type 'ListViewItem' is not defined

Can anybody please let me know How can I migrate ?

Best Regards
Mrutyunjaya

Developer technologies | .NET | .NET Runtime
Developer technologies | VB
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2021-04-19T11:31:34.71+00:00

    Hi,
    please check count:

    If ListView1.SelectedItems.Count > 0 Then
      Dim lvi As ListViewItem = ListView1.SelectedItems(0)
      Debug.Print(lvi.Text)
    End If
    

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-04-20T11:50:53.577+00:00

    This is what will work with a VB .NET project set to use .NET 5 without adding any references to the project.

    Full source

    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>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.