Marco
Marco

Reputation: 85

vb.net loop through selected items Listbox

I am trying to loop through all selected items of my ListBox1. The problem is my code keeps the same 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

Upvotes: 0

Views: 587

Answers (1)

JayV
JayV

Reputation: 3271

with the line:

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

You are ignoring the item in the loop and going for the first selected item.

You should replace this with:

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

The variable Item is one you declared in the For Loop

Upvotes: 1

Related Questions