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

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

问题描述

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

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

我在第一个Next上方添加了ListBox1.Tag = sDir行,当我逐步执行代码时,sDir的值似乎可以保存路径,但是如果我创建一个弹出的简单Double click事件,一个带有文件路径的消息框仅显示列表中的第一个文件夹名称.

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天全站免登陆