如何在 Visual Basic 6 中为每个列表项命名

发布于 2024-12-02 18:50:48 字数 709 浏览 1 评论 0原文

我正在使用列表控件制作音乐播放器。我想让用户更改列表中歌曲的名称,但我希望 THAT 列表项的某些属性包含其路径。 请帮助我。任何形式的帮助将不胜感激。提前致谢。

编辑

Private Sub AddToList(ByVal txtFileName As String)
    Dim I As Integer
    Dim blnFileAlreadyexists As Boolean
    txtFileName = Trim(txtFileName)
    If txtFileName <> "" Then
        blnFileAlreadyexists = False
        For I = 0 To List1.ListCount - 1
            If Trim(List1.List(I)) = txtFileName Then
                blnFileAlreadyexists = True
            End If
        Next
        If Not blnFileAlreadyexists Then
            List1.AddItem (txtFileName)
            List1.ItemData (txtFileName)

        End If
    End If
End Sub

I am making a music player using the list control. I want to let the user change the name of the song on the list, but i want some property of THAT list item to contain its path.
Please help me in this. Any kind of help will be appreciated. Thanks in advance.

EDIT

Private Sub AddToList(ByVal txtFileName As String)
    Dim I As Integer
    Dim blnFileAlreadyexists As Boolean
    txtFileName = Trim(txtFileName)
    If txtFileName <> "" Then
        blnFileAlreadyexists = False
        For I = 0 To List1.ListCount - 1
            If Trim(List1.List(I)) = txtFileName Then
                blnFileAlreadyexists = True
            End If
        Next
        If Not blnFileAlreadyexists Then
            List1.AddItem (txtFileName)
            List1.ItemData (txtFileName)

        End If
    End If
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2024-12-09 18:50:48

对于列表框,添加项目后,将其x.itemdata(x.newindex)设置为包含相应数据的数组(或UDT数组)的索引。

对于listview,您可以类似地使用单个项目.Tag.Key来存储数组(或集合)索引。

链接列表框示例

Option Explicit

Private Type TFileData
    OriginalFilePath As String
    ListBoxIndex     As Integer
    MoreBlaBla       As String
    '//any more members
End Type

Private maFiles() As TFileData

Private Sub Form_Load()
    '//initial alloc
    ReDim maFiles(0)

    AddToList "AAAA"
    AddToList "BBBB"
    AddToList "AAAA"
    AddToList "CCCC"

    '//test by looping listbox;
    Dim i As Integer
    For i = 0 To List1.ListCount - 1
         MsgBox List1.List(i) & " - " & maFiles(List1.ItemData(i)).OriginalFilePath
    Next

    '// a better type centric test;
    For i = 0 To UBound(maFiles) - 1
        MsgBox maFiles(i).OriginalFilePath & " - List entry: " & List1.List(maFiles(i).ListBoxIndex)
    Next
End Sub

Private Sub AddToList(ByVal txtFileName As String)
    Dim i As Integer
    Dim blnFileAlreadyexists As Boolean
    txtFileName = Trim(txtFileName)
    If txtFileName <> "" Then
        blnFileAlreadyexists = False
        For i = 0 To List1.ListCount - 1
            If Trim(List1.List(i)) = txtFileName Then
                blnFileAlreadyexists = True
            End If
        Next
        If Not blnFileAlreadyexists Then
            '//add to list
            List1.AddItem (txtFileName)

            '//store the original value in the array;
            maFiles(UBound(maFiles)).OriginalFilePath = "TEST: " & txtFileName

            '//store the index of the array in the list;
            List1.ItemData(List1.NewIndex) = UBound(maFiles)

            '//or better store in the type
            maFiles(UBound(maFiles)).ListBoxIndex = List1.NewIndex

            '//increment the array for the next item;
            ReDim Preserve maFiles(UBound(maFiles) + 1)
        End If
    End If
End Sub

For a listbox, after you add an item set its x.itemdata(x.newindex) to the index of an array (or UDT array) that contains the corresponding data.

For a listview you can similarly use an individual items .Tag or .Key to store an array (or collection) index.

Linking a listbox example;

Option Explicit

Private Type TFileData
    OriginalFilePath As String
    ListBoxIndex     As Integer
    MoreBlaBla       As String
    '//any more members
End Type

Private maFiles() As TFileData

Private Sub Form_Load()
    '//initial alloc
    ReDim maFiles(0)

    AddToList "AAAA"
    AddToList "BBBB"
    AddToList "AAAA"
    AddToList "CCCC"

    '//test by looping listbox;
    Dim i As Integer
    For i = 0 To List1.ListCount - 1
         MsgBox List1.List(i) & " - " & maFiles(List1.ItemData(i)).OriginalFilePath
    Next

    '// a better type centric test;
    For i = 0 To UBound(maFiles) - 1
        MsgBox maFiles(i).OriginalFilePath & " - List entry: " & List1.List(maFiles(i).ListBoxIndex)
    Next
End Sub

Private Sub AddToList(ByVal txtFileName As String)
    Dim i As Integer
    Dim blnFileAlreadyexists As Boolean
    txtFileName = Trim(txtFileName)
    If txtFileName <> "" Then
        blnFileAlreadyexists = False
        For i = 0 To List1.ListCount - 1
            If Trim(List1.List(i)) = txtFileName Then
                blnFileAlreadyexists = True
            End If
        Next
        If Not blnFileAlreadyexists Then
            '//add to list
            List1.AddItem (txtFileName)

            '//store the original value in the array;
            maFiles(UBound(maFiles)).OriginalFilePath = "TEST: " & txtFileName

            '//store the index of the array in the list;
            List1.ItemData(List1.NewIndex) = UBound(maFiles)

            '//or better store in the type
            maFiles(UBound(maFiles)).ListBoxIndex = List1.NewIndex

            '//increment the array for the next item;
            ReDim Preserve maFiles(UBound(maFiles) + 1)
        End If
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文