vb.net 遍历所选项目列表框

vb.net loop through selected items Listbox

我正在尝试遍历我的 ListBox1 中所有选定的项目。问题是我的代码保持不变 SearchFolder = ItemSelected

If ListBox1.SelectedIndex > -1 Then
                For Each Item As Object In ListBox1.SelectedItems

                    Dim ItemSelected = CType(ListBox1.SelectedItem.Item("Path"), String)
                    SearchFolder = ItemSelected

                    Dim dirInfo As New IO.DirectoryInfo(SearchFolder)
                    Dim files As IO.FileInfo() = dirInfo.GetFiles()
                    Dim file As IO.FileInfo

                    docImage = (ImageList1.Images.Count - 1)
                    Dim items As New List(Of ListViewItem)

                    For Each file In files
                        Dim filename As String = file.Name.ToString

                        If file.Extension = ".pdf" Then
                            foundList = PDFManipulation.GetTextFromPDF2(SearchFolder + filename, SearchRegX)
                            If foundList = True Then
                                items.Add(New ListViewItem(New String() {"", filename.ToString}, docImage))
                            End If
                        End If
                    Next
                    ListView1.Items.AddRange(items.ToArray)

                Next
            End If

行:

Dim ItemSelected = CType(ListBox1.SelectedItem.Item("Path"), String)

您正在忽略循环中的项目并转到第一个选定的项目。

您应该将其替换为:

Dim ItemSelected = CType(Item("Path"), String)

变量 Item 是您在 For Loop

中声明的变量