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

发布于 2024-11-03 16:21:37 字数 2445 浏览 5 评论 0原文

我想使用自定义类存储有关列表视图项目的附加信息,但我似乎无法让它工作。我目前正在使用此代码来使用列表框项目完成类似的操作。我只想用列表视图做同样的事情。

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

所以我的问题是如何使用列表视图来完成此操作?这是我尝试过的:

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’”

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想念有你 2024-11-10 16:21:37

让您的 Class myListboxItem 继承自 ListViewItem。

Make your Class myListboxItem inherit from ListViewItem.

半﹌身腐败 2024-11-10 16:21:37

我知道这个话题已经有两年了,但我遇到它是为了寻找答案。我自己尝试了一下,它在 .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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文