创建一个自定义listViewItem类以存储其他隐藏的信息-VB .Net [英] Create a custom listViewItem class to store additional hidden info - VB .Net

查看:72
本文介绍了创建一个自定义listViewItem类以存储其他隐藏的信息-VB .Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自定义类存储有关listview项的其他信息,但是我似乎无法使其正常工作.我目前正在使用此代码使用列表框项来完成类似的操作.我只想对列表视图做同样的事情.

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

我这样设置属性

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

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

The error I get is "Value of type 'System.Windows.Forms.listView.SelectedListViewItemCollection' cannot be converted to 'MyProject.myListViewItem"

推荐答案

使您的Class myListboxItem从ListViewItem继承.

Make your Class myListboxItem inherit from ListViewItem.

这篇关于创建一个自定义listViewItem类以存储其他隐藏的信息-VB .Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆