将文件路径添加到列表框项 [英] Adding file path to listbox item

查看:27
本文介绍了将文件路径添加到列表框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件路径存储在列表框项的标记中.

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

我已将 ListBox1.Tag = sDir 行添加到第一个 Next 上方,当我逐步浏览代码时,sDir 似乎保留了路径,但是如果我创建一个简单的 双击 事件,该事件会弹出一个带有文件路径的消息框,它只会显示列表中的第一个文件夹名称.

I've added the ListBox1.Tag = sDir line to above the first Next and when I step thorugh the code the value of sDir appears to hold the path however if I create a simple Double click event that pops up a message box with the file path in it only shows the first folder name in the list.

任何提示或建议 - 我基本上想选择一个列表框项目并让它指向它的路径!

Any tips or advice - I basically want to select a Listbox item and have it point to its path!

谢谢

 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

推荐答案

你可以将对象存储为项目,所以一个小类来存储项目信息:

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)

并将其取回:

 ' 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)

这篇关于将文件路径添加到列表框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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