创建自定义 listViewItem 类来存储其他隐藏信息 - VB .Net
我想使用自定义类存储有关列表视图项目的附加信息,但我似乎无法让它工作。我目前正在使用此代码来使用列表框项目完成类似的操作。我只想用列表视图做同样的事情。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让您的
Class myListboxItem
继承自 ListViewItem。Make your
Class myListboxItem
inherit from ListViewItem.我知道这个话题已经有两年了,但我遇到它是为了寻找答案。我自己尝试了一下,它在 .net 2010 上运行得很好:
它可能对某人有帮助:)
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 :)