lAdding files to a two column listbox

后端 未结 1 1137
执念已碎
执念已碎 2021-01-26 11:49

I am trying to add files to a list box in the same way as they are added when files are attached to an email, in two columns but able to address each file uniquely. I am using A

相关标签:
1条回答
  • 2021-01-26 12:18

    Try mapping the indices of the items in the listBox to the files using a separate array/collection/dictionary. Then, when an item is selected, you can use the item's index to get the link

    I just tried this below, works perfectly.
    Userform Code:

    Private d As Dictionary
    
    Private Sub userform_initialize()
        Set d = New Dictionary
    
        'populate list box, and add items to dictionary
        For i = 1 To 3
            With ListBox1
                d.Add .ListCount, "Link" & i
                .AddItem 0
                .List(.ListCount - 1) = "Hello World"
            End With
        Next
    End Sub
    
    'update label1's value based of listbox's selected item
    Private Sub ListBox1_Change()
        With ListBox1
            If .ListIndex <> -1 Then
                Label1.Caption = d.Item(.ListIndex)
            End If
        End With
    End Sub
    

    And the userform looked like this: (listbox named ListBox1, and label named Label1):
    enter image description here

    Example:
    enter image description here

    If you have problems using the dictionary object, you need to add a reference to "Microsoft Scripting Runtime". But really any collection-type can replace the dictionary in this case (an array/Collection)

    0 讨论(0)
提交回复
热议问题