创建一个自定义listViewItem类来存储其他隐藏信息 - VB .Net

时间:2011-04-29 00:35:26

标签: vb.net class listview override listviewitem

我想使用自定义类存储有关listview项的其他信息,但我似乎无法使其工作。我目前正在使用此代码使用列表框项目来完成类似的操作。我只想用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

我设置了这样的属性

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

然后我可以使用以下方法访问这些附加属性:

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

所以我的问题是如何使用listview完成这项工作?这是我试过的:

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

我得到的错误是“类型的值'System.Windows.Forms.listView.SelectedListViewItemCollection'无法转换为'MyProject.myListViewItem”

2 个答案:

答案 0 :(得分:2)

从ListViewItem继承Class myListboxItem

答案 1 :(得分:1)

我知道这个主题是2岁,但我遇到它寻找答案。刚试过自己,它可以正常使用.net 2010: 它可能会帮助某人:)


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