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"