Create a custom listViewItem class to store additi

2019-06-03 07:30发布

I want to store additional information about a listview item using a custom class, but I can't seem to get it to work. I'm currently using this code to accomplish something similar using a listbox item. I just want to do the same thing with a listview.

Public Class myListboxItem
   Public id As String
   Public rootFolder As String
   Public name As String
   Public info() As String
   Public Text As String
   Public Overrides Function ToString() As String
       Return Me.Text
   End Function
End Class

I set the properties like this

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim item As New myListboxItem
    item.Text = "This is a Test"
    item.rootFolder = "C:\test"
    item.id = "testid"
    item.name = "Test Item"
    item.info(0) = "Some Information"
    lstExample.Items.Add(item)
End Sub

Then I can access these additional properties using this:

Private Sub lstExample_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstExample.SelectedIndexChanged
    Dim item As myListboxItem = CType(lstExample.SelectedItem, myListboxItem)
    messagebox.show(item.id)
    messagebox.show(item.rootFolder)
    messagebox.show(item.name)
    messagebox.show(item.info(0))
End sub

So my question is how can this be done with a listview? Here is what I tried:

Public Class myListViewItem
   Public id As String
   Public rootFolder As String
   Public name As String
   Public info() As String
   Public Text As String
   Public Overrides Function ToString() As String
       Return Me.Text
   End Function
End Class

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim item As New myListViewItem
    item.Text = "This is a Test"
    item.rootFolder = "C:\test"
    item.id = "testid"
    item.name = "Test Item"
    item.info(0) = "Some Information"
    lsvExample.Items.Add(item)
End Sub

Private Sub lsvExample_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvExample.SelectedIndexChanged
    'problem with the next line
    Dim item As myListViewItem = CType(lsvExample.SelectedItems, myListViewItem)
    'tried this too.. similar error
    Dim item2 As myListViewItem = CType(lsvExample.SelectedItems(0), myListViewItem)
    messagebox.show(item.id)
    messagebox.show(item.rootFolder)
    messagebox.show(item.name)
    messagebox.show(item.info(0))
End sub

The error I get is "Value of type 'System.Windows.Forms.listView.SelectedListViewItemCollection' cannot be converted to 'MyProject.myListViewItem"

2条回答
【Aperson】
2楼-- · 2019-06-03 07:40

Make your Class myListboxItem inherit from ListViewItem.

查看更多
地球回转人心会变
3楼-- · 2019-06-03 07:50

I know this topic is 2 years old, but I come accross it looking for an answer. Just tried myself and it works fine with .net 2010: It might help someone :)


Public Class ListViewItemExtra
    Inherits ListViewItem
    Private _companyName As String = ""
    Private _contactPerson As String = ""
    Private _address As String = ""
    Public Property CompanyName As String
        Get
            Return _companyName
        End Get
        Set(value As String)
            _companyName = value
            Text = ToString()
        End Set
    End Property
    Public Property ContactPerson As String
        Get
            Return _contactPerson
        End Get
        Set(value As String)
            _contactPerson = value
            Text = ToString()
        End Set
    End Property
    Public Property Address As String
        Get
            Return _address
        End Get
        Set(value As String)
            _address = value
            Text = ToString()
        End Set
    End Property
    Public Overrides Function ToString() As String
        Return _companyName + " -> " & _address.Replace(vbCrLf, " ")
    End Function
    Public Function ToPrintString() As String
        Dim heading As String = ""
        If _contactPerson  "" Then
            heading = "Attention: " & _contactPerson & vbCrLf & vbCrLf
        End If
        Return heading & _companyName & vbCrLf & _address
    End Function
End Class
查看更多
登录 后发表回答