Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a custom listViewItem class to store additional hidden info - VB .Net
    text
    copied!<p>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.</p> <pre><code>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 </code></pre> <p>I set the properties like this</p> <pre><code>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 </code></pre> <p>Then I can access these additional properties using this:</p> <pre><code>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 </code></pre> <p>So my question is how can this be done with a listview? Here is what I tried:</p> <pre><code>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 </code></pre> <p>The error I get is "Value of type 'System.Windows.Forms.listView.SelectedListViewItemCollection' cannot be converted to 'MyProject.myListViewItem"</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload