Adding file path to listbox item

后端 未结 1 906
栀梦
栀梦 2021-01-27 15:10

I\'m trying to store the file path in a tag of a listbox item.

I\'m using the below to search through and add the desired folder name to the list box

I\'ve added

相关标签:
1条回答
  • 2021-01-27 15:48

    You can store objects as items, so a small class to store item info:

    Public Class myClass
        Public Property FileName as String
        Public Property PathName As String
        Public Foo As Integer
    
        ' class is invalid w/o file and path:
        Public Sub New(fName As String, pName As String)
             FileName = FName
             PathName = pName
        End Sub
    
    
        ' this will cause the filename to show in the listbox
        Public Overrides Function ToString() AS String
             Return FileName
        End Sub
     End Class
    

    You can now store these in the listbox as you load/find them:

     Dim El as MyClass           ' temp var for posting to listbox
    
     ' in the loop:
     El = New MyClass(filename, pathName)    ' use names from your Dir/File objects
     ListBox1.Items.Add(El)
    

    and to get it back:

     ' INDEX_TO_READ is a dummy var of the index you want to get
     ' SelectedItem will also work
     thisFile = Ctype(ListBox1.Items(INDEX_TO_READ), MyClass).FileName
     thisPath = Ctype(ListBox1.Items(INDEX_TO_READ), MyClass).PathName
     ' or:
     Dim aFile As myClass = Ctype(ListBox1.Items(INDEX_TO_READ), MyClass)
    
    0 讨论(0)
提交回复
热议问题