我试图将文件路径存储在列表框项目的标签中。
我正在使用下面的内容进行搜索并将所需的文件夹名称添加到列表框中
我已经添加了ListBox1.Tag = sDir
行到第一行上方Next
当我单步执行代码时,值sDir
似乎保留了路径,但是如果我创建一个简单的Double click
弹出一个消息框,其中包含文件路径的事件仅显示列表中的第一个文件夹名称。
任何提示或建议 - 我基本上想选择一个列表框项目并让它指向它的路径!
Thanks
For Each Dir As String In System.IO.Directory.GetDirectories("c:\Working")
Dim dirInfo As New System.IO.DirectoryInfo(Dir)
For Each sDir As String In System.IO.Directory.GetDirectories(dirInfo.ToString)
Dim sdirInfo As New System.IO.DirectoryInfo(sDir)
ListBox1.Items.Add(sdirInfo.Name)
ListBox1.Tag = sDir
Next
Next
您可以将对象存储为项目,因此使用一个小类来存储项目信息:
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
现在,您可以在加载/查找它们时将它们存储在列表框中:
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)
并把它找回来:
' 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)